File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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
You canโt perform that action at this time.
0 commit comments