문제
Given a string 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.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
풀이
class Solution:
def isValid(self, s: str) -> bool:
l = []
for ch in s :
if ch == '(' or ch == '{' or ch == '[' :
l.append(ch)
else :
if len(l) == 0 :
return False
elif ch == ')' :
if l.pop() != '(' :
return False
elif ch == '}' :
if l.pop() != '{' :
return False
elif ch == ']' :
if l.pop() != '[' :
return False
return len(l) == 0
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
27. Remove Element (0) | 2020.05.06 |
---|---|
26. Remove Duplicates from Sorted Array (0) | 2020.05.05 |
9. Palindrome Number (0) | 2020.05.04 |
1. Two Sum (0) | 2020.05.03 |
14.Longest Common Prefix (0) | 2020.05.03 |