Leetcode 20. Valid Parentheses
Aug 16, 2021
[easy]
https://leetcode.com/problems/valid-parentheses/
Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([)]"
Output: false
Example 5:
Input: s = "{[]}"
Output: true
[Java]
- Using stack to solve this leetcode. If we find the open parentheses, we push them into the stack; if we find the close parentheses, we peek the stack, and if it’s a match, pop from the stack.
- return false if something else appear in the string, or return true in the end.