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
+ Intuition:
5
+ stack ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํด์ ๋ซํ๋ ๊ดํธ๊ฐ ์ฌ ๊ฒฝ์ฐ
6
+ stack์ ๋ง์ง๋ง๊ณผ ์ผ์นํ๋์ง ํ์ธํ๋ค.
7
+
8
+ Time Complexity:
9
+ O(N):
10
+ ๋ฌธ์์ด์ ํ๋ฒ ์ค์บํ๋ฉด์ ์กฐ๊ฑด๋ฌธ์ ํ์ธํ๋ฏ๋ก
11
+ O(N)์ ์๊ฐ๋ณต์ก๋๊ฐ ์์๋๋ค.
12
+
13
+ Space Complexity:
14
+ O(N):
15
+ ์ต์
์ ๊ฒฝ์ฐ ๋ฌธ์์ด ๊ฐ์๋งํผ stack์ ์ ์ฅํ๋ค.
16
+ """
17
+ stack = []
18
+ for ch in s :
19
+ if ch in ["(" , "{" , "[" ]:
20
+ stack .append (ch )
21
+ elif ch in [")" , "}" , "]" ]:
22
+ if stack and (
23
+ (ch == ")" and stack [- 1 ] == "(" )
24
+ or (ch == "}" and stack [- 1 ] == "{" )
25
+ or (ch == "]" and stack [- 1 ] == "[" )
26
+ ):
27
+ stack .pop ()
28
+ else :
29
+ return False
30
+
31
+ if stack :
32
+ return False
33
+ else :
34
+ return True
You canโt perform that action at this time.
0 commit comments