알고리즘 문제/LEETCODE
[Python] Valid Parentheses
이융희
2021. 5. 6. 01:52
728x90
Easy 난이도 Acceptence 40.1% 문제
괄호의 짝이 맞는지 안맞는지를 판정하는 문제로
리스트를 스택처럼 사용하였다.
괄호 왼쪽일경우 리스트에 더하고 오른쪽일 경우 pop하여 Last in 한값과 비교하도록 하였고 비교값을 Askii 코드 값을 사용하였다.
class Solution(object):
def __init__(self):
self.output = []
self.left = "([{"
self.right = ")]}"
self.valid = [81, 184, 248]
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
for word in s:
if word in self.left:
self.output.append(ord(word))
if word in self.right:
if not self.output:
return False
if self.output.pop() + ord(word) not in self.valid:
return False
return not bool(self.output)