FunctionRacerProblemsRace

Minimum Add to Make Parentheses Valid

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

s has only ( and ). You may insert parentheses anywhere. Return the fewest insertions that make s a valid parentheses string, where every ( is closed in order by a later ) and every ) closes an earlier (.

Example 1

s "())"
returns 1

Add one ( at the front.

Example 2

s "((("
returns 3

Three ) are needed.

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