Skip to content

Commit 27f07b2

Browse files
committed
week 6 valid-parentheses
1 parent c8051cb commit 27f07b2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

valid-parentheses/i-mprovising.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Time complexity O(n)
3+
4+
stack
5+
"""
6+
class Solution:
7+
def isValid(self, s: str) -> bool:
8+
open_brackets = ["(", "{", "["]
9+
bracket_map = {"(":")", "{":"}", "[":"]"}
10+
stack = []
11+
for char in s:
12+
if char in open_brackets:
13+
stack.append(char)
14+
continue
15+
if not stack:
16+
return False
17+
open_b = stack.pop() # last in open bracket
18+
if bracket_map[open_b] != char:
19+
return False
20+
if stack:
21+
return False
22+
return True
23+
24+

0 commit comments

Comments
 (0)