LeetCode 20. Valid Parentheses



Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.


The problem is about validating parenthesis in the input string. The input string contains only '(', ')', '{', '}', '[' and ']'.

By introducing a stack, we could easily solve the problem.

Start from the first position to visit every character in the string:

  • If the character is one left parenthesis, we push it to stack
  • If the character is one right parenthesis, we check if the character matches with the left parenthesis at the top of the stack. If not matched or the stack is empty, the input string fails the test.

Video Tutorial

Java Solution

Leave a Reply

Your email address will not be published. Required fields are marked *