smj007

Untitled

Jun 19th, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. class Solution:
  2.     def isValid(self, string: str) -> bool:
  3.          
  4.             stack = []
  5.             mapping = {
  6.                 "}" : "{",
  7.                 ")" : "(",
  8.                 "]" : "["
  9.             }
  10.            
  11.             for s in string:
  12.                 if s in ["{", "(", "["]:
  13.                     stack.append(s)
  14.                 elif s in ["}", ")", "]"]:
  15.                     if len(stack) > 0 and stack[-1] == mapping[s]:
  16.                         stack.pop()
  17.                     else:
  18.                         return False
  19.                 else:
  20.                     continue
  21.                    
  22.             # return only if stack is empty
  23.             return (len(stack) == 0)
Advertisement
Add Comment
Please, Sign In to add comment