본문 바로가기
python-algorithm

leetcode 20. Valid Parentheses

by 무적김두칠 2023. 2. 23.

https://leetcode.com/problems/valid-parentheses/description/

 

Valid Parentheses - LeetCode

Can you solve this real interview question? 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 sam

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
    def isValid(self, s: str-> bool:
        stack = []
        table = {
            ')':'(',
            '}':'{',
            ']':'['
        }
        for char in s:
            if char not in table:
                stack.append(char)
            elif not stack or table[char] != stack.pop():
                return False
        
        return len(stack) == 0
cs

전형적인 스택 문제 입니다!

This is typical problem using 'stack'

반응형

'python-algorithm' 카테고리의 다른 글

leetcode 739. Daily Temperatures  (0) 2023.02.23
leetcode 316. Remove Duplicate Letters  (1) 2023.02.23
백준 9773 ID Key  (0) 2023.02.21
leetcode 92. Reverse Linked List II  (0) 2023.02.17
leetcode 328. Odd Even Linked List  (0) 2023.02.17

댓글