FunctionRacerProblemsRace

Balanced Outside Quotes

MediumStacks and queuesPython, JavaScript, TypeScript, Java, C++

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

code "f(\"(\", [1, 2])"
returns true

The ( inside the quotes is text, so only (, [, ], ) count, and they match.

Example 2

code "g(\")\""
returns false

( opens, then the quoted ) is text, so nothing closes the bracket.

Example 3

code "\"abc"
returns false

The string literal is never closed.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems