Description
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
My solution
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for i in s:
if i in ('(', '[', '{'):
stack.append(i)
elif len(stack) == 0 and i in (')', ']', '}'):
stack.append(i)
break
elif i == ')' and stack[-1] == '(':
stack.pop()
elif i == ']' and stack[-1] == '[':
stack.pop()
elif i == '}' and stack[-1] == '{':
stack.pop()
else:
break
return True if len(stack) == 0 else False
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
[Leetcode] Easy : Remove Duplicates from Sorted Array (0) | 2021.09.01 |
---|---|
[프로그래머스] level2. 더 맵게 (0) | 2021.09.01 |
[프로그래머스] level2. 124 나라의 숫자 (0) | 2021.08.30 |
[Leetcode] Easy : Roman to Integer (0) | 2021.08.30 |
[Leetcode] Easy : Palindrome Number (0) | 2021.08.22 |