10 LINE concise/readable JAVA with GOOD FORMAT


  • 0
    M
        public boolean isValid(String s) {
            Stack<Character> st = new Stack();
            String openParens = "([{", closeParens = ")]}";
            for (char ch : s.toCharArray()) {
                if (openParens.contains(String.valueOf(ch))) {
                    st.push(ch);
                } else if (st.empty() || openParens.indexOf(st.pop()) != closeParens.indexOf(ch)) {
                    return false;
                }
            } 
            return st.isEmpty();
        }
    

Log in to reply
 

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