Wednesday, October 24, 2018

Valid Parentheses




def is_valid_parentheses(s):
    stack = []
    dict = {"]":"[", "}":"{", ")":"("}
    count =0
    for char in s:
        count+=1
        if char in dict.values():
            stack.append(char)
        elif char in dict.keys():
            if stack == [] or dict[char] != stack.pop():
                return False
        else:
            return False
    return stack == []
is_valid_parentheses("{}")

Link to Github
Debug

No comments:

Post a Comment