Skip to content

Commit fcfd652

Browse files
author
sejineer
committed
valid-parentheses solution
1 parent c51b8bd commit fcfd652

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

valid-parentheses/sejineer.py

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

0 commit comments

Comments
 (0)