[LeetCode] Valid Parentheses
Updated:
문제
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.
예제
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([)]"
Output: false
Example 5:
Input: s = "{[]}"
Output: true
조건
- 1 <= s.length <= $10^4$
s
consists of parentheses only'()[]{}'
.
답
풀이 1
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for i in s:
if stack:
if i == ']' and stack[-1] == '[':
stack.pop()
elif i == ')' and stack[-1] == '(':
stack.pop()
elif i == '}' and stack[-1] == '{':
stack.pop()
else:
stack.append(i)
else:
stack.append(i)
return len(stack) == 0
Leave a comment