알고리즘 - 유효한 괄호
괄호로 된 입력값이 올바른지 판별하라.
Input | Output |
---|---|
s = “()[]{}” | true |
s = “{[]}” | true |
s = “([)]” | false |
Solution1 (스택 일치 여부 판별)
class Solution:
def isValid(self, s: str) -> bool:
stack = []
table = {
')': '(',
']': '[',
'}': '{'
}
for char in s:
if char not in table:
stack.append(char)
elif not stack or table[char] != stack.pop():
return False
return len(stack) == 0
댓글남기기