-
leetcode.com/problems/valid-parentheses/
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.
class Solution: def isValid(self, s: str) -> bool: #뚜껑과 그릇이 딱 만나는 순간에도 같아야한다 stack = [] for char in s: if char == "(" or char == "{" or char == "[": stack.append(char) if char == ")" : #testcase ")"때문에 if not stack or stack[-1] != "(": return False stack.pop() if char == "}" : if not stack or stack[-1] != "{": return False stack.pop() if char == "]" : if not stack or stack[-1] != "[": return False stack.pop() if stack: # testcase "["때문에 return False return True
'2020년 > 코테' 카테고리의 다른 글
[코테 연습] Product of Array Except Self (0) 2020.09.24 [코테 연습] Fair Candy Swap (0) 2020.09.24 [코테 연습] All Elements in Two Binary Search Trees (0) 2020.09.16 [코테 연습] Sort the Matrix Diagonally (0) 2020.09.16 [코테 연습] 구명 보트 (0) 2020.09.15