EASY 0020. Valid Parenthesis¶
Problem Statement¶
Examples¶
Solution¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | """Solution for the Leetcode problem #0020 Valid Parenthesis
https://leetcode.com/problems/valid-parenthesis/
"""
def is_valid(s: str) -> bool:
from collections import deque
string_stack = deque()
for character in s:
if character in ["[", "{", "("]:
string_stack.append(character)
elif character in ["]", "}", ")"]:
if len(string_stack) == 0:
return False
last = string_stack.pop()
if character == "[":
if last != "]":
return False
elif character == "{":
if last != "}":
return False
elif character == ")":
if last != "(":
return False
return True
|