Simple 6 line Solution in Java


  • 0
    G

    
        public boolean isValid(String s) {
            final String open = "({[", close = ")}]";
            Stack<Character> stack = new Stack<>();
            for(char c : s.toCharArray())
                if (open.indexOf(c) != -1 ) stack.push(c);
                else if ( stack.isEmpty() || open.indexOf(stack.pop()) != close.indexOf(c) ) return false;
            return stack.isEmpty();
        }
    


Log in to reply
 

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