File tree 3 files changed +75
-1
lines changed
Stack/4_3_valid_parentheses
3 files changed +75
-1
lines changed Original file line number Diff line number Diff line change 41
41
### 연결리스트
42
42
- [ 3.4 연결리스트 뒤집기] ( LinkedList/3_4_revert_list/README.md )
43
43
- [ 3.5 순환 검출] ( LinkedList/3_5_cycle_detect/README.md )
44
- - [ 3.6 두 수 더하기] ( LinkedList/3_6_add_two_num/README.md )
44
+ - [ 3.6 두 수 더하기] ( LinkedList/3_6_add_two_num/README.md )
45
+ ### 스택
46
+ - [ 4.3 유효한 괄호 검증] ( Stack/4_3_valid_parentheses/README.md )
Original file line number Diff line number Diff line change
1
+ ## 유효한 괄호 검증
2
+ - Page 223
3
+ ### Brute-force
4
+ - [ valid_parentheses_bf.py] ( valid_parentheses_bf.py )
5
+
6
+ ### LeetCode
7
+ - https://leetcode.com/problems/valid-parentheses/
8
+
9
+
10
+
11
+
12
+
Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+ tests = {
4
+ 1 : "()" ,
5
+ 2 : "()[]{}" ,
6
+ 3 : "(]" ,
7
+ 4 : "([)]" ,
8
+ 5 : "{[]}" ,
9
+ 6 : "(" ,
10
+ 7 : "]" ,
11
+ 8 : "((){})"
12
+ }
13
+
14
+ res = {
15
+ 1 : True ,
16
+ 2 : True ,
17
+ 3 : False ,
18
+ 4 : False ,
19
+ 5 : True ,
20
+ 6 : False ,
21
+ 7 : False ,
22
+ 8 : True
23
+ }
24
+
25
+ def check_result (index : int , output : int ):
26
+ if index > len (tests ):
27
+ raise RuntimeError (f'Failed to get { index } th case' )
28
+ return res .get (index , False ) == output
29
+
30
+ def isValid (s : str ) -> bool :
31
+ stack = []
32
+
33
+ paren_map = {
34
+ ')' : '(' ,
35
+ '}' : '{' ,
36
+ ']' : '['
37
+ }
38
+
39
+ for ch in s :
40
+ if ch not in paren_map .keys ():
41
+ stack .append (ch )
42
+ else :
43
+ pair = stack .pop () if stack else ''
44
+
45
+ if paren_map [ch ] != pair :
46
+ return False
47
+
48
+ return len (stack ) == 0
49
+
50
+ def main ():
51
+ for index , input_string in tests .items ():
52
+ res = isValid (input_string )
53
+
54
+ if check_result (index , res ):
55
+ print (f'Test case { index } is correct: value { res } ' )
56
+ else :
57
+ print (f'Test case { index } is failed: value { res } ' )
58
+
59
+ if __name__ == '__main__' :
60
+ main ()
You can’t perform that action at this time.
0 commit comments