Here is another illustration that shows how to manage various types of brackets (not just parentheses): def are_brackets_balanced ( s ) : stack = [ ] bracket_map = { "(" : ")" , "[" : "]" , "{" : "}" } open_brackets = set ( [ "(" , "[" , "{" ] ) for char in s : if char in open_brackets : stack . append ( char ) elif stack and char == bracket_map [ stack [ - 1 ] ] : stack . pop () else : return False return not stack This function, like the previous one, initializes an empty stack along with a mapping of open and closed bracket types, as well as a set for quick access to the open bracket types. It then iterates through each character in the string, checking if it is an open bracket. If it is, the character is added to the stack. When encountering a closing bracket, it checks the top element of the stack to see if it corresponds, and if so, removes it from the stack. If there is a mismatch (or if the stack is already empty), this indicates that there is a closing bracket without a matching opening bracket, and the function returns False. Similarly, if the stack is empty after processing all characters, it returns True, confirming that all brackets were correctly paired.