Given a string s containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
class Solution:
def isValid(self, s: str) -> bool:
stack = []
brackets = {
'}' : '{',
')' : '(',
']' : '['
}
for w in s:
if w in brackets.values(): # 여는 괄호
stack.append(w)
else:
if stack and brackets[w] == stack[-1]:
stack.pop()
else:
return False
if stack:
return False
return True