6 line Java Solution, using 1 Stack

  • 0
    public static boolean isValidzz(String s) {
    	Stack<Character> set = new Stack<Character>();
    	for(int i = 0; i < s.length(); i++){
    		if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[') set.push(s.charAt(i));
    	else if(!set.isEmpty() && ((int)s.charAt(i))-((int)set.peek())<3 && ((int)s.charAt(i))-((int)set.peek())>0) set.pop();
    	else return false;}
    	return set.empty();

Log in to reply

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