-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path20.py
More file actions
21 lines (21 loc) · 771 Bytes
/
20.py
File metadata and controls
21 lines (21 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def isValid(self, s: str) -> bool:
bracketStack = []
for i in range(len(s)):
if (s[i] == "("):
bracketStack.append(")")
elif (s[i] == "{"):
bracketStack.append("}")
elif (s[i] == "["):
bracketStack.append("]")
elif (len(bracketStack) == 0):
return False
elif (s[i] == ")" and bracketStack[-1] == ")"):
bracketStack.pop()
elif (s[i] == "}" and bracketStack[-1] == "}"):
bracketStack.pop()
elif (s[i] == "]" and bracketStack[-1] == "]"):
bracketStack.pop()
else:
return False
return len(bracketStack) == 0