<jupyter_start><jupyter_text>Validate Balanced Parentheses[The Original Question](https://mp.weixin.qq.com/s/PW7Lqurlde-xTyatiJUlGQ)  QuestionGiven a string containing just the characters `(`,  `)`,  `{`,  `}`,  `[` and `]`,  determine if the input string is valid. An input string is valid if:- Open brackets are closed by the same type of brackets.- Open brackets are closed in the correct order.- An empty string.<jupyter_code>def validation(brackets: str) -> bool:

    # Define a dictionary to attach the bracket pairs.
    pairs = {
        '}': '{',
        ']': '[',
        ')': '('
    }

    # Create a stack to record unclosed brackets.
    stack = list()

    # Traverse all characters in the string.
    for k in brackets:

        # If the current characters closes the nearest bracket,
        # remove it from the stack, representing it is closed.
        if len(stack) > 0 and k in pairs.keys() and pairs[k] == stack[-1]:
            stack.pop()
            pass

        # Otherwise, push the current characters into the stack,
        # and mark them unclosed.
        else:
            stack.append(k)
            pass
        pass

    # If input string is valid,
    # there's no unclosed characters remained in the stack.
    return len(stack) == 0
print(validation(r''))
print(validation(r'([{}])'))
print(validation(r'(){}[]'))
print(validation(r'{](}[)'))
print(validation(r')]}'))<jupyter_output>True
True
True
False
False
{'fraction_non_alphanumeric': 0.12758620689655173, 'fraction_numerical': 0.002758620689655172, 'mean_word_length': 2.7885117493472587, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '606404', 'license': 'MIT', 'n_tokens_mistral': 450, 'n_tokens_neox': 423, 'n_words': 148, 'path': 'January/Week1/4.ipynb', 'repo_name': 'Dragon1573/Daily-Problem', 'size': '1450'}