My java solution with stack


  • 0
    L
    public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        stack.push('*');
        for(int i=0;i<s.length();i++){
            char c=s.charAt(i);
            char topChar=stack.peek();
            if(c==')'&&topChar=='('||c=='}'&&topChar=='{'||c==']'&&topChar=='[') stack.pop();
            else stack.push(c);
        }
        return stack.peek()=='*';
    }
    

    }


Log in to reply
 

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