Valid Parentheses
๊ทธ๋ฅ ๊ดํธ์ ์ง์ ๊ฒ์ฌํ๋ ๋ฌธ์ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ์คํ์ ์ด์ฉํ๊ณ
์ด๋ฆฐ ๊ดํธ์, ๋ซํ ๊ดํธ๋ฅผ ๊ตฌ๋ถํ์ฌ ์กฐ๊ฑด์ ๋ฐ๋ผ
push, popํ๋ค.
class Solution(object): def isValid(self, s): """ :type s: str :rtype: bool """ stack = [] for c in s: if len(stack) == 0 and c in [')', '}', ']']: return False elif c in ['(', '{', '[']: stack.append(c) elif c == ')': if stack[-1] == '(': stack.pop(-1) else: return False elif c == '}' : if stack[-1] == '{': stack.pop(-1) else: return False elif c == ']' : if stack[-1] == '[': stack.pop(-1) else: return False if len(stack) == 0: return True else: return False