Created
June 10, 2025 18:52
-
-
Save maptv/ed14054de48a010bac0432d753609ad2 to your computer and use it in GitHub Desktop.
Check if wrapping characters in a string is balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_balanced(s: str) -> bool: | |
stack = [] | |
bracket_map = {')': '(', ']': '[', '}': '{'} | |
open_brackets = set(bracket_map.values()) | |
for char in s: | |
if char in open_brackets: | |
stack.append(char) | |
elif char in bracket_map: | |
if not stack or stack[-1] != bracket_map[char]: | |
return False | |
stack.pop() | |
return not stack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment