33 lines
680 B
Python
33 lines
680 B
Python
def isValid(s):
|
|
"""
|
|
:type s: str
|
|
:rtype: bool
|
|
"""
|
|
pervious = ""
|
|
openBracket = ['(', '{', '[']
|
|
bracketPairs = ['()', '{}', '[]']
|
|
pairs = None
|
|
|
|
for x in s:
|
|
if x in openBracket:
|
|
previous = x
|
|
else:
|
|
# print(previous+x)
|
|
if (previous+x) in bracketPairs:
|
|
pairs = True
|
|
else:
|
|
return False
|
|
return pairs
|
|
|
|
print(isValid("()"))
|
|
print(isValid("()[]{}"))
|
|
print(isValid("(]"))
|
|
print(isValid("([)]"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|
|
print(isValid("{[]}"))
|