My Java Solution


  • 0
    B

    public static boolean isValid(String s) {

    	 Stack<Character> stack=new Stack<Character>();
    	 Hashtable<Character, Character> hashtable=new Hashtable<>();
    	 hashtable.put('(', ')');
    	 hashtable.put('{', '}');
    	 hashtable.put('[', ']');
    	 char[] input=s.toCharArray();
    	 for(int i=input.length-1;i>=0;i--)
    	 {
    		 if(input[i]==')'||input[i]=='}'||input[i]==']')
    		 {
    			 stack.push(input[i]);
    		 }
    		 else {
    			 Character temp=null;
    			 if(stack.empty())
    				 return false;
    			 else {
    				temp=stack.pop();
    			}
    			if(hashtable.get(input[i])!=temp)
    				return false;
    				
    		}
    	 }
    	 if(stack.empty())
    		 return true;
    	 else {
    		return false;
    	}
    	 
            
        }

Log in to reply
 

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