1ms Straightforward java solution


  • 0
    J
    public class Solution {
        public boolean isValid(String s) {
            Stack<Character> st = new Stack<Character>();
            for(int i = 0; i < s.length(); i++){
                if(s.charAt(i) == ')'){
                    if(st.size() != 0 && st.peek() == '('){ 
                        st.pop(); 
                        continue;
                    }else{
                        return false;
                    }
                }
                if(s.charAt(i) == ']'){
                    if(st.size() != 0 && st.peek() == '['){ 
                        st.pop();
                        continue;
                    }else{
                        return false;
                    }
                }
                if(st.size() != 0 && s.charAt(i) == '}'){
                    if(st.peek() == '{'){ 
                        st.pop();
                        continue;
                    }else{
                        return false;
                    }
                }
                st.push(s.charAt(i));
            }
            if(st.size() == 0) return true;
            return false;
        }
    }

Log in to reply
 

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