Skip to content

Commit 95d9c8a

Browse files
committed
feat: week6 - valid parentheses
1 parent de096ec commit 95d9c8a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
20. Valid Parentheses
3+
Stack์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ์ œ์ž„์„ ํŒŒ์•…ํ•˜์ง€ ๋ชปํ•ด์„œ ๊ฒฐ๊ตญ ์•Œ๊ณ ๋‹ฌ๋ ˆ๋‹˜์˜ ๋„์›€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.
4+
5+
Time Complexity: O(n)
6+
Space Complexity: O(n)
7+
'''
8+
class Solution:
9+
def isValid(self, s: str) -> bool:
10+
parens = {"(": ")", "{": "}", "[": "]"}
11+
stack = []
12+
for val in s:
13+
if val in parens:
14+
stack.append(val)
15+
else:
16+
if not stack or val != parens[stack.pop()]:
17+
return False
18+
return not stack

0 commit comments

Comments
ย (0)