class Solution:
def isValid(self, s: str) -> bool:
d={"(":")","{":"}","[":"]"}
stack=[]
for p in s:
if p in d: #check each paremnthasis if it is opening one
stack.append(d[p]) # append closing of that to stack
elif stack and stack[-1]==p: #if p is closing parethasis and
stack.pop()#it is last ele in stack pop that ele
else:
return False
return True if not stack else False
#time complexity O(n)
# memeory complexity O(n)
Comments
Post a Comment