Skip to content

Commit d1f6b81

Browse files
committed
valid-parentheses solution
1 parent d4a1e12 commit d1f6b81

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

โ€Žvalid-parentheses/yyyyyyyyyKim.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
4+
# Stack
5+
stack = []
6+
i = 0
7+
8+
while i < len(s):
9+
# ์—ฌ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ ์Šคํƒ์— ์ถ”๊ฐ€
10+
if s[i] == "(" or s[i] == "{" or s[i] == "[":
11+
stack.append(s[i])
12+
13+
# ๋‹ซ๋Š” ๊ด„ํ˜ธ์ผ ๊ฒฝ์šฐ
14+
else:
15+
# ์Šคํƒ์ด ๋น„์–ด ์žˆ์„๊ฒฝ์šฐ ์ง์ด ๋งž์ง€์•Š์œผ๋ฏ€๋กœ False ๋ฆฌํ„ด
16+
if not stack:
17+
return False
18+
19+
# ์Šคํƒ ๋งˆ์ง€๋ง‰์— ์ง๋งž๋Š” ์—ฌ๋Š” ๊ด„ํ˜ธ๊ฐ€ ์—†์œผ๋ฉด False
20+
if s[i] == ")" and stack[-1] != "(":
21+
return False
22+
elif s[i] == "}" and stack[-1] != "{":
23+
return False
24+
elif s[i] == "]" and stack[-1] != "[":
25+
return False
26+
27+
# ์ง์ด ๋งž์œผ๋ฉด pop
28+
stack.pop()
29+
30+
# ๋‹ค์Œ ๊ธ€์ž๋กœ ์ด๋™
31+
i += 1
32+
33+
# ๋ฌธ์ž์—ด์„ ๋‹ค ๋Œ๊ณ , ์Šคํƒ๋„ ๋น„์–ด์žˆ๋‹ค๋ฉด True ๋ฆฌํ„ด
34+
return not stack

0 commit comments

Comments
ย (0)