Solution in java


  • 0
    A
    class Solution {
        public boolean isValid(String s) {
            Stack<Character> para = new Stack<Character> ();
           for(char c  : s.toCharArray()){
                if(c == '(' || c == '{' || c == '['){
                    para.push(c);
                }else if (c == ')'){
                    if(!para.empty() && para.peek() == '('){
                        para.pop();
                    }else {
                        return false ;
                    }
                }else if ( c == '}'){
                    if(!para.empty() && para.peek() == '{'){
                        para.pop();
                    }else {
                        return false ;
                    }
                }else {
                    if(!para.empty() && para.peek() == '['){
                        para.pop();
                    }else {
                        return false ;
                    }
                }
            }
            
           if (para.empty())  return true ;
           return false ;
        }
    }
    

Log in to reply
 

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