-
Notifications
You must be signed in to change notification settings - Fork 43
/
valid-parentheses.py
177 lines (155 loc) · 4.18 KB
/
valid-parentheses.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Given a string containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
"""
# V0
# IDEA : STACK + DICT
class Solution(object):
def isValid(self, s):
# edge case
if not s:
return True
if len(s)==1 or len(s) % 2 == 1:
return False
q = []
d = {"(":")", "[":"]", "{":"}"}
for i in s:
if i in d.keys():
q.append(i)
else:
if not q:
return False
tmp = q.pop(-1)
if d[tmp] != i:
return False
return True if not q else False
# V0'
# IDEA : STACK + DICT
class Solution:
# @return a boolean
def isValid(self, s):
stack, lookup = [], {"(": ")", "{": "}", "[": "]"}
for parenthese in s:
if parenthese in lookup:
stack.append(parenthese)
elif len(stack) == 0 or lookup[stack.pop()] != parenthese:
return False
return len(stack) == 0
# V0''
# IDEA : queue + dict
class Solution(object):
def isValid(self, s):
d = {'(':')', '{':'}', '[':']'}
q = []
if not s:
return True
if len(s) == 1:
return False
for i in s:
if i in d:
q.append(i)
else:
if not q:
return False
else:
tmp = q.pop(-1)
if d[tmp] != i:
return False
return True if not q else False
# V0'''
# IDEA : STACK + DICT
class Solution:
def isValid(self, s):
lookup = {"(":")", "[":"]", "{":"}"}
q = []
for i in s:
if i not in lookup and len(q) == 0:
return False
elif i in lookup:
q.append(i)
else:
tmp = q.pop()
if lookup[tmp] != i:
return False
return True if len(q) == 0 else False
# V1
# https://blog.csdn.net/coder_orz/article/details/51697963
# IDEA : STACK
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
pars = [None]
parmap = {')': '(', '}': '{', ']': '['}
for c in s:
if c in parmap and parmap[c] == pars[len(pars)-1]:
pars.pop()
else:
pars.append(c)
return len(pars) == 1
# V1'
# https://blog.csdn.net/coder_orz/article/details/51697963
# IDEA : STACK
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
pars = [None]
parmap = {')': '(', '}': '{', ']': '['}
for c in s:
if c in parmap:
if parmap[c] != pars.pop():
return False
else:
pars.append(c)
return len(pars) == 1
# V1''
class Solution:
# @return a boolean
def isValid(self, s):
stack, lookup = [], {"(": ")", "{": "}", "[": "]"}
for parenthese in s:
if parenthese in lookup:
stack.append(parenthese)
elif len(stack) == 0 or lookup[stack.pop()] != parenthese:
return False
return len(stack) == 0
# if __name__ == "__main__":
# print(Solution().isValid("()[]{}"))
# print(Solution().isValid("()[{]}"))
# V2
# Time: O(n)
# Space: O(n)
class Solution(object):
# @return a boolean
def isValid(self, s):
stack, lookup = [], {"(": ")", "{": "}", "[": "]"}
for parenthese in s:
if parenthese in lookup:
stack.append(parenthese)
elif len(stack) == 0 or lookup[stack.pop()] != parenthese:
return False
return len(stack) == 0