8 line sexy Java solution!


  • 0
    H

    Keep code sexy.

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<Character>();
        for (char c : s.toCharArray()) {
            if (c == '{') { stack.push('}'); continue; }
            if (c == '[') { stack.push(']'); continue; }
            if (c == '(') { stack.push(')'); continue; }
            if (stack.isEmpty() || stack.pop() != 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.