-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode-Solution-N20.py
More file actions
45 lines (36 loc) · 1.46 KB
/
LeetCode-Solution-N20.py
File metadata and controls
45 lines (36 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# LeetCode problem N20: Valid Parentheses
# Difficulty: Easy
class Solution:
def isValid(self, s: str) -> bool:
# Step 1: Initialize a stack to keep track of opening parentheses
stack = []
# Step 2: Create a mapping of closing parentheses to their corresponding opening parentheses
CO = {
")" : "(",
"}" : "{",
"]" : "["
}
# Step 3: Iterate through each character in the string
for c in s:
# Step 4: If the character is a closing parenthesis, check if it matches the top of the stack
if c in CO:
if stack and stack[-1] == CO[c]:
stack.pop()
else:
return False
# Step 5: If the character is an opening parenthesis, push it onto the stack
else:
stack.append(c)
# Step 6: If the stack is empty, all parentheses were matched; otherwise, return False
return True if not stack else False
s = "()"
s1 = "()[]{}"
s2 = "(]"
s3 = "([])"
sol = Solution()
print(sol.isValid(s)) # Output: True
print(sol.isValid(s1)) # Output: True
print(sol.isValid(s2)) # Output: False
print(sol.isValid(s3)) # Output: True
# Time Complexity: O(n), where n is the length of the string.
# Space Complexity: O(n), for the stack used to store opening parentheses.