Java Solution in 5 ms | Stack


  • 0
    S
    public class Solution {
        public boolean isValid(String s) {
         
         int size = s.length();
         int t1=0;
         char[] stack1 = new char[size];
         for(int i=0; i < size;i++){
         char a = s.charAt(i);
         
        switch(a){
                case '}' : if(t1==0 || stack1[t1-1]!='{'){
                                return false;
                            }else{
                                t1--;
                            } break;
                            
                case ']' : if(t1==0 || stack1[t1-1]!='['){
                                return false;
                            }else{
                                t1--;
                            } break;
                            
                case ')' : if(t1==0 || stack1[t1-1]!='('){
                                return false;
                            }else{
                                t1--;
                            } break;
                default : stack1[t1++] = a;
            }
         }
         return (t1 ==0 ) ? true : false;
        }
    }
    

Log in to reply
 

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