EASY NC#21 Blind #55 Stack
20. Valid Parentheses
๐ Problem
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. 3. Every close bracket has a corresponding open bracket of the same type.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
๐ก Approach
- โ Use stack to track opening brackets
- โ When closing bracket appears, check if top matches
- โ At the end, stack should be empty
- โ Time: O(n), Space: O(n)
๐ ๏ธ Hints & Pitfalls
Hints
- โขUse stack to track opening brackets
- โขWhen closing bracket appears, check if top matches
- โขAt the end, stack should be empty
Common Pitfalls
- โขTime: O(n), Space: O(n)
๐งช Test Cases
Hidden tests on submit: 5
Test Case 1
Not run Input:
isValid('()'); Expected:
true Test Case 2
Not run Input:
isValid('()[]{}'); Expected:
true Test Case 3
Not run Input:
isValid('(]'); Expected:
false ๐ Code Editor
๐ค Output