Skip to content

Commit 4376514

Browse files
author
jinbeom
committed
Valid Parentheses Solution
1 parent fdbc181 commit 4376514

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

valid-parentheses/kayden.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 시간복잡도: O(N)
2+
# 공간복잡도: O(N)
3+
class Solution:
4+
def isValid(self, s: str) -> bool:
5+
if len(s) % 2 != 0:
6+
return False
7+
8+
stack = []
9+
matching_brackets = {'(': ')', '{': '}', '[': ']'}
10+
11+
for char in s:
12+
if char in matching_brackets:
13+
stack.append(char)
14+
elif stack and matching_brackets[stack[-1]] == char:
15+
stack.pop()
16+
else:
17+
return False
18+
19+
return not stack

0 commit comments

Comments
 (0)