Balanced Outside Quotes
code is one line of source text. It may contain the brackets (), [] and {}, double quotes ", and any other characters. A double quote starts a string literal that runs up to the next double quote; brackets inside a string literal are plain text and do not count.
Outside string literals the brackets must be balanced: each closing bracket must match the most recent bracket that is still open, which must be of the same kind, and nothing may stay open at the end. Return True when the brackets outside the literals are balanced and every string literal is closed, otherwise False. Every character that is not a bracket or a double quote is ignored.
Example 1
The ( inside the quotes is text, so only (, [, ], ) count, and they match.
Example 2
( opens, then the quoted ) is text, so nothing closes the bracket.
Example 3
The string literal is never closed.
Constraints
0 <= len(code) <= 60codehas printable ASCII characters only
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Browser Back and ForwardMedium
- Convoys at the GateMedium
- Daily TemperaturesMedium
- Decode StringMedium
- Flip and Double BracketsMedium
- Keep the Digits, One of Each LetterMedium
- Largest Open PlotMedium
- Longest Balanced PieceMedium