FunctionRacerProblemsRace

Plus Minus With Parentheses

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

expr is an arithmetic expression made of non-negative integers, the operators + and -, round brackets and spaces. Brackets may be nested, and a - may stand directly before a bracket, as in 3 - (1 + 2). The expression is valid: it and the inside of every bracket start with a number or an opening bracket, so there are no unary signs. Operators of the same level are applied from left to right.

Return the value of the expression as an integer. It may be negative.

Example 1

expr "3 - (1 + 2)"
returns 0

The bracket is worth 3, and 3 - 3 is 0.

Example 2

expr "(1+2)-(3-(4-5))"
returns -1

(4-5) is -1, so (3-(4-5)) is 4, and 3 - 4 is -1.

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