01 · Question
Given a string and a list of two-character bracket pairs, return whether its brackets are balanced. Each pair defines an opening character followed by its closer; no character is repeated anywhere in the pair list. Non-bracket characters are ignored. Matching pairs must be properly nested.
Examples: (a+[b]) with ["()", "[]"] → true; ([)] with those pairs → false; )( with [")("] → true; hello with an empty pair list → true.
02 · Analysis
Push the expected closing character whenever an opener appears. For a closer, reject if the stack is empty or the top differs; otherwise pop. At the end require an empty stack.
Trace for ([)]: push ), push ], then reject ) because ] is expected first. Counting brackets cannot detect this crossing. O(n + b) expected time and O(n + b) space, with n input characters and b bracket pairs, using hash maps/sets. Go uses runes to support non-ASCII bracket characters.