Leetcode 20. Valid Parentheses

Mingyu Jeon·2020년 4월 25일
0
post-thumbnail

# Runtime: 32 ms, faster than 39.39% of Python3 online submissions for Valid Parentheses.
# Memory Usage: 14 MB, less than 5.22% of Python3 online submissions for Valid Parentheses.
class Solution:
    def isValid(self, s: str) -> bool:
        hashMap = {
            '(':')',
            '{':'}',
            '[':']'
        }
        stack = []
        if len(s) % 2 != 0: return False
        if len(s) == 0: return True
        for i in range(len(s)):
            if s[i] == '(' or s[i] == '{' or s[i] == '[':
                stack.append(hashMap[s[i]])
            
            else:
                if not stack: return False
                close_parentheses = stack.pop()
                if s[i] != close_parentheses:
                    return False
        if not stack:
            return True
        else:
            return False                

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

0개의 댓글