· 2 min read

Blind 75 - Valid Parentheses

God-level solution - iterate.<br>If an opening bracket is found, put the closing bracket in a stack.<br>If the closing bracket is found, pop and check for equality.

Link

Learning From

NeetCode

Asked at

Source

Video

https://youtu.be/7KHKFqLPMbs

Problem and Constraints

All Approaches and Explanations in English

My solution

put if opening bracket put into the stack. If it is the closing bracket pop, check if the bracket on top is the closing bracket of the current. In the end, check if the stack is empty.

Code, if any

class Solution {
    public boolean isValid(String s) {
        Deque<Character> stack = new ArrayDeque<>();
        for(Character c: s.toCharArray()){
            if(c == '(' || c == '{' || c == '['){
                stack.addLast(c);
                continue;
            }
                        
            if(stack.size()==0){
                return false;
            }
            
            Character top = stack.removeLast();
            if(c == ']' && top!='['){
                return false;
            }
            if(c == ')' && top!='('){
                return false;
            }
            if(c == '}' && top!='{'){
                return false;
            }
        }
            System.out.println(stack);
        return stack.size() == 0;
    }
}

God-Level Solution

iterate. if an opening bracket is found, put the closing bracket in a stack. if the closing bracket is found, pop and check for equality.

Source

maybe i am a idiot,i should give up cs

same 9 years education, why are u outstanding ?

boolean isValid(String s) {
  if ((s.length() & 1) == 1) return false;
  else {
    Deque<Character> p = new ArrayDeque<>(s.length());
    for (int i = 0; i < s.length(); i++)
      switch (s.charAt(i)) {
        case '(': p.push(')'); break;
        case '{': p.push('}'); break;
        case '[': p.push(']'); break;
        case ')': case '}': case ']': if (p.isEmpty() || p.pop() != s.charAt(i)) return false;
      }
    return p.isEmpty();
  }
}

Back to Blog