File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - opening(` ([{ ` ) ์คํ์ ์๋๋ค.
3
+ - ๋น๊ตํ๋ค.
4
+
5
+ ### Approach
6
+ 1 . ๋จผ์ ์
๋ ฅ๋ฐ์ ๊ธ์์ ๊ธธ์ด๊ฐ ์ง์๊ฐ ์๋๋ฉด ๋ฌด์กฐ๊ฑด ** False** .
7
+ 2 . for ๋ฌธ
8
+ - opening(` ([{ ` ) ์ด๋ฉด stack ์ ๋ฃ๋๋ค.
9
+ - stack ์ด ๋น์ด์๊ฑฐ๋, ์ง์ด ๋ง์ง ์์ผ๋ฉด(` is_parentheses()==False ` ) ** False** .
10
+ 3 . ๋ค ์๋ฃ๋๊ณ ๋์, ์คํ์ด ๋น์ด์๋ค๋ฉด ** True** .
11
+
12
+
13
+ ### Complexity
14
+ - Time complexity: O(n)
15
+ - Space complexity: O(n)
16
+
17
+
18
+ ### Code
19
+
20
+ ``` python
21
+ class Solution :
22
+ def isValid (self , s : str ) -> bool :
23
+ stack = []
24
+ if len (s) % 2 != 0 :
25
+ return False
26
+ for word in s:
27
+ if word in ' ([{' :
28
+ stack.append(word)
29
+ elif not stack or is_parentheses(stack.pop(), word) is False :
30
+ return False
31
+ return not stack
32
+
33
+
34
+ def is_parentheses (pointer , word ) -> bool :
35
+ if pointer == ' (' and word == ' )' :
36
+ return True
37
+ elif pointer == ' [' and word == ' ]' :
38
+ return True
39
+ elif pointer == ' {' and word == ' }' :
40
+ return True
41
+ else : return False
42
+ ```
You canโt perform that action at this time.
0 commit comments