-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.py
76 lines (58 loc) · 2.18 KB
/
20.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''20. Valid Parentheses
Created on 2024-12-30 14:54:41
2024-12-30 15:34:48
@author: MilkTea_shih
'''
#%% Packages
#%% Variable
#%% Functions
class Solution_reference:
def isValid(self, s: str) -> bool:
if len(s) % 2:
return False # There is at least 1 bracket that is not closed.
left_brackets: list[str] = []
bracket_paired_table: dict[str, str] = {
')': '(',
']': '[',
'}': '{',
}
for bracket in s:
if bracket not in bracket_paired_table: #append left bracket
left_brackets.append(bracket)
elif (not left_brackets # `bracket` is the right bracket.
or left_brackets.pop() != bracket_paired_table[bracket]):
return False # `bracket` is paired with the wrong bracket.
return not left_brackets # True: len(left_brackets) == 0, else False.
class Solution:
def isValid(self, s: str) -> bool:
if len(s) % 2:
return False # There is at least 1 bracket that is not closed.
left_brackets: list[str] = []
for bracket in s:
match bracket:
case '(' | '[' | '{': #append left bracket
left_brackets.append(bracket)
case _: # The right bracket is paired with the left bracket.
if left_brackets:
match left_brackets.pop(): #paired in ordered
case '(':
if bracket != ')':
break
case '[':
if bracket != ']':
break
case '{':
if bracket != '}':
break
#
else: # There is no left bracket paired with right's.
break
#
else:
return False if left_brackets else True #check `left_brackets`
return False
#%% Main Function
#%% Main
if __name__ == '__main__':
pass
#%%