<--- 리트코드 20. Valid Parentheses --->

# 문제: 괄호로 된 입력 값이 올바른지 판별하라.

 

입력

()[]{}

출력

true

 

 

# 풀이

1. 스택 일치 여부 판별

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def is_valid(s):
  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 
 
 
= "()[]{}"
print(is_valid(s))
cs