-
90. Valid ParenthesesProblem Solving 2023. 1. 19. 03:50728x90
https://leetcode.com/problems/valid-parentheses
Valid Parentheses - LeetCode
Valid Parentheses - Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be
leetcode.com
괄호 여닫기 문제
class Solution: def isValid(self, s: str) -> bool: stack = [] for i in s: if i in '([{': stack.append(i) elif len(stack) == 0: return False elif i == ')' and stack[-1] == '(': stack.pop() elif i == ']' and stack[-1] == '[': stack.pop() elif i == '}' and stack[-1] == '{': stack.pop() else: return False if len(stack) == 0: return True return False
괄호 여닫기와 비슷한 문제들을 여럿 풀었더니 면역이 됐나 싶음
'Problem Solving' 카테고리의 다른 글
92. Number of 1 Bits (0) 2023.01.19 91. Two Sum II - Input Array Is Sorted (0) 2023.01.19 89. Unique Morse Code Words (0) 2023.01.19 88. 욱제는 효도쟁이야!! (0) 2023.01.19 87. 등장하지 않는 문자의 합 (0) 2023.01.18