O(n) Java Solution


  • 0
    R
    class Solution {
        public boolean isValid(String s) {
            if(s.length()<=1) return false;
            Stack<Character> paraS = new Stack<Character>();
            for(int i=0;i<s.length();i++){
                if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='['){
                    paraS.push(s.charAt(i));
                }else if(s.charAt(i)==')'){
                    if(paraS.size()==0 || paraS.pop()!= '(') return false;
                }else if(s.charAt(i)==']'){
                    if(paraS.size()==0 || paraS.pop()!= '[') return false;
                }else if(s.charAt(i)=='}'){
                    if(paraS.size()==0 || paraS.pop()!= '{') return false;
                }
            }
         
            return paraS.isEmpty();
        }
    } ```

Log in to reply
 

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