Problem 20: Valid Parentheses
Question
LeetCode Link | 20. Valid Parentheses | Easy
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.
- Every close bracket has a corresponding open bracket of the same type.
Constrains
1 <= s.length <= 10^4
s
consists of parentheses only'()[]{}'
.
Examples
Example 1
Input: s = “()”
Output: true
Example 2
Input: s = “()[]{}”
Output: true
Example 3
Input: s = “(]”
Output: false
Solution Approach
Method: Stack
Constrains
- None
This method does not have any limitations.
Concept Explanation
Due to the special characteristics of stack structures, they are very well-suited for symmetry matching problems.
First, it’s important to understand the different scenarios in which parentheses in a string may not match.
It is recommended to analyze the different types of mismatches before writing code. If these scenarios are not well understood beforehand, the resulting code may have many issues.
Let’s analyze the three types of mismatches:
- Scenario 1: There are extra left-facing parentheses in the string, resulting in a mismatch.
Scenario 2: There are no extra parentheses, but the types of parentheses do not match. ![image/image-20240510184737733.png)
Scenario 3: There are extra right-facing parentheses in the string, resulting in a mismatch.
As long as our code covers these three types of mismatches, it will be robust. This highlights the importance of thoroughly analyzing the problem before coding.
- Scenario 1: After traversing the entire string, if the stack is not empty, it means there are unmatched left parentheses, so return false.
- Scenario 2: During the traversal and matching process, if the stack does not have the character to be matched, return false.
- Scenario 3: During the traversal and matching process, if the stack is already empty and there are no characters left to match, it means a right parenthesis has not found its corresponding left parenthesis, so return false.
When are all left and right parentheses matched? It’s when, after traversing the entire string, the stack is empty, indicating that all parentheses are matched.
Code
- Time complexity: O(n)
- Space complexity: O(n)
TypeScript
1 | function isValid(s: string): boolean { |
Conclusion
Parentheses matching is a classic problem solved using stacks.
The problem is akin to ensuring the order of parentheses in our code is correct: a left parenthesis must have a corresponding right parenthesis in the appropriate position.
If you recall compiler theory, during lexical analysis, compilers handle parentheses, braces, and similar symbols using stack data structures.
Another example is the Linux cd
command, which we are quite familiar with:
1 | cd a/b/c/../../ |
This command ultimately navigates to the a
directory. How does the system know it has navigated to the a
directory? This is an application of stacks (this could actually be a relevant interview question).
Thus, stacks are widely used in the field of computer science.