給定一個只包括 '(',')','{','}','[',']' 的字符串,判斷字符串是否有效。
有效字符串需滿足:
左括號必須用相同類型的右括號閉合。
左括號必須以正確的順序閉合。
注意空字符串可被認為是有效字符串。
class Solution:
def isValid(self, s):
if len(s) == 0:
return True
count = 0
length = len(s)
while (count < length/2):
s = s.replace('()', '').replace('[]', '').replace('{}', '')
count += 1
if len(s) == 0:
break
if len(s) > 0:
return False
else:
return True