We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard stack problem, graded against 7 test cases (4 of them hidden).
Last-in-first-out processing - matching, nesting, and deferred evaluation.
Reach for it when you see: Balanced brackets, nested structures, undo/back semantics, or expression evaluation.
More Stackproblems →Scan the string once, maintaining a running `result`, the `sign` to apply to the next term, and the number being parsed. On `(`, push the current `result` and `sign` onto a stack and start a fresh subexpression; on `)`, finish the subexpression and combine it with the popped context: `result = savedResult + savedSign * inner`.
Unary minus falls out naturally: `-( ... )` simply records `sign = -1` before the parenthesis is opened. Multi-digit numbers are accumulated digit by digit, and spaces are skipped.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.