Share my simple java sol


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

Log in to reply
 

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