My java clean code


  • 0
    M

    public class Solution {
    public boolean isValid(String s) {
    if(s.length() == 0)
    return true;
    char temp = '0';
    Stack<Character> stack = new Stack<Character>();

        for(int i = 0; i < s.length(); i++) {
            temp = s.charAt(i);
            if(temp == '(' || temp == '{' || temp == '[')
                stack.push(temp);
            else{
                if(stack.empty())
                    return false;
                if(temp == ')' && stack.peek() != '(')
                    return false;
                if(temp == '}' && stack.peek() != '{')
                    return false;
                if(temp == ']' && stack.peek() != '[')
                    return false;
                stack.pop();
            }
        }
    
        return stack.empty();
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.