class Solution:
def isValid(self, s):
if len(s)%2==1:
return False
else:
while ('()' in s) or ('[]' in s) or ('{}' in s):
s = s.replace('()','').replace('[]','').replace('{}','')
return (s=='')
8 line solution in python

@Zoulfwin86 this still works, cause there is always at least on pair of '()' '[]' '{}' in the string if the string is valid. it will remove '()' '[]' '{}' from inside to outside.


@heretoinfinity
Python's replace function takes O(n) time to execute since it creates a copy of the string and returns it.In a valid string of parenthesis there are n/2 valid open/close parenthesis pairs. (e.g "({})" has "{}" "()" or "(())" has an inner "()" and an outer "()" )
So the replace function is called at least n/2 times which results in a n*(n/2) complexity which is O((n^2)/2) but in Asymptotic Complexity we ignore coefficients leaving us with a O(n^2) time complexity.
