文章

20-valid-parentheses

20-valid-parentheses

Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type.

Example 1:

Input: s = “()”

Output: true

Example 2:

Input: s = “()[]{}”

Output: true

Example 3:

Input: s = “(]”

Output: false

Example 4:

Input: s = “([])”

Output: true

My solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        for el in s:
            if el == '(' or el == '[' or el == '{':
                stack.append(el)
            else:
                if len(stack) == 0:
                    return False
                if el == ')' and stack.pop() != '(':
                    return False
                elif el == ']' and stack.pop() != '[':
                    return False
                elif el == '}' and stack.pop() != '{':
                    return False
        return True if len(stack) == 0 else False

Time Complexity: O(n) Space Complexity: O(n)

本文由作者按照 CC BY 4.0 进行授权