Skip to content

Commit 892a5b1

Browse files
committed
feat: implement function to validate bracket pairs in a string
1 parent a7790ff commit 892a5b1

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

valid-parentheses/kangdaia.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
"""s에 있는 bracket들의 쌍이 맞는지 확인하는 함수
4+
시간 복잡도: O(n), 공간 복잡도: O(n)
5+
6+
Args:
7+
s (str): bracket {}, [], ()로 이루어진 문자열
8+
9+
Returns:
10+
bool: bracket들의 쌍이 맞는지 여부
11+
"""
12+
stack = []
13+
pair = {"{": "}", "[": "]", "(": ")"}
14+
for ch in s:
15+
if ch in pair:
16+
stack.append(ch)
17+
elif len(stack) == 0:
18+
return False
19+
else:
20+
last = stack.pop()
21+
if pair[last] != ch:
22+
return False
23+
return True if len(stack) == 0 else False

0 commit comments

Comments
 (0)