Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Tags: String
Try It!
Discussion
Video
Solution
class Solution:
def isValid(self, s: str) -> bool:
pair = {
')': '(',
'}': '{',
']': '['
}
stack = []
for c in s:
if c in pair:
if not stack:
return False
top = stack.pop()
if top != pair[c]:
return False
else:
stack.append(c)
return len(stack) == 0