unozerocode / exercises

Various algorithm exercises and a list of books
Mozilla Public License 2.0
0 stars 1 forks source link

Check balanced parentheses #4

Closed unozerocode closed 5 years ago

unozerocode commented 5 years ago

Hi, here's your problem today. This problem was recently asked by Uber:

Imagine you are building a compiler. Before running any code, the compiler must check that the parentheses in the program are balanced. Every opening bracket must have a corresponding closing bracket. We can approximate this using strings.

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if:

Example: Input: "((()))" Output: True

Input: "[()]{}" Output: True

Input: "({[)]" Output: False

class Solution:
def isValid(self, s):
    # Fill this in.

# Test Program
s = "()(){(())" 
# should return False
print(Solution().isValid(s))

s = ""
# should return True
print(Solution().isValid(s))

s = "([{}])()"
# should return True
print(Solution().isValid(s))