Skip to content

Commit 2a7520f

Browse files
committed
feat: add valid parentheses
1 parent fe1bac7 commit 2a7520f

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Problem 0020: Valid Parentheses
2+
3+
## Link
4+
https://leetcode.com/problems/valid-parentheses/
5+
6+
## Intuition
7+
Use a stack to ensure every opening bracket has a matching closing bracket in the correct order.
8+
9+
## Approach
10+
Iterate through the string, pushing opening brackets onto a stack and popping to match each closing bracket; return false if a mismatch or leftover remains.
11+
12+
## Complexity
13+
- Time: O(n)
14+
- Space: O(n)
15+
16+
## Notes
17+
- Handle cases where the string starts with a closing bracket or ends with unmatched openings.
18+
- Use a helper function to map each closing bracket to its corresponding opening bracket.
19+
- Works for all three bracket types: (), {}, [].
20+
- Returns false immediately on any mismatch or if the stack is not empty at the end.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from collections import deque
2+
3+
4+
class Solution:
5+
def isValid(self, s: str) -> bool:
6+
result = deque()
7+
8+
for c in s:
9+
if c in "({[":
10+
result.append(c)
11+
else:
12+
if result:
13+
left = result.pop()
14+
if left != self.getLeft(c):
15+
return False
16+
else:
17+
return False
18+
19+
return not result
20+
21+
def getLeft(self, c):
22+
if c == ")":
23+
return "("
24+
elif c == "}":
25+
return "{"
26+
return "["
27+
28+
29+
if __name__ == "__main__":
30+
s = Solution()
31+
print(s.isValid("()")) # Expected: True
32+
print(s.isValid("()[]{}")) # Expected: True
33+
print(s.isValid("(]")) # Expected: False
34+
print(s.isValid("([])")) # Expected: True
35+
print(s.isValid("(()")) # Expected: False
36+
print(s.isValid("(")) # Expected: False
37+
print(s.isValid("]")) # Expected: False

problems/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
| Number | Title | NeetCode 150 | Link | Folder |
44
| ------ | ----- | ------------ | ---- | ------ |
55
| 0001 | Two Sum | true | [Link](https://leetcode.com/problems/two-sum/) | [0001-two-sum](0001-two-sum) |
6+
| 1 | Two Sum | false | [Link](https://leetcode.com/problems/two-sum/) | [1-two-sum](1-two-sum) |
67
| 0019 | Remove Nth Node From End Of List | true | [Link](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [0019-remove-nth-node-from-end-of-list](0019-remove-nth-node-from-end-of-list) |
8+
| 0020 | Valid Parentheses | true | [Link](https://leetcode.com/problems/valid-parentheses/) | [0020-valid-parentheses](0020-valid-parentheses) |
79
| 0021 | Merge Two Sorted Lists | true | [Link](https://leetcode.com/problems/merge-two-sorted-lists/) | [0021-merge-two-sorted-lists](0021-merge-two-sorted-lists) |
810
| 0141 | Linked List Cycle | true | [Link](https://leetcode.com/problems/linked-list-cycle/) | [0141-linked-list-cycle](0141-linked-list-cycle) |
911
| 0217 | Contains Duplicate | true | [Link](https://leetcode.com/problems/contains-duplicate/) | [0217-contains-duplicate](0217-contains-duplicate) |

0 commit comments

Comments
 (0)