2Brute2Best
🔥0

Basic Calculator

Hard·LeetCode #224 Pro OnlyInterview-Level
LeetCode
01

Problem Statement

You are given a string s that holds an arithmetic expression with brackets. Return its value.

  • The expression has non-negative integers, +, -, (, ) and spaces. There is no * or /.
  • - can be used as a unary minus, like "-1" or "-(2 + 3)". + is never unary.
  • Two operators never appear next to each other (but "1-(-2)" is fine, because there is a bracket in between).
  • You may not use a built-in function like eval().
Input

A string s made of digits, + - ( ) and spaces. It is always a valid expression.

Output

The integer value of the expression.

Example 1
Input: s = "1 + 1"
Output: 2
Example 2
Input: s = " 2-1 + 2 "
Output: 3

Left to right: 2 − 1 = 1, then 1 + 2 = 3.

Example 3
Input: s = "(1+(4+5+2)-3)+(6+8)"
Output: 23

The inner bracket (4+5+2) is 11, so the first group is 1 + 11 − 3 = 9. The second group is 14. 9 + 14 = 23.

Constraints
  • • 1 ≤ s.length ≤ 3 · 10^5
  • • `s` contains only digits, `+ - ( )` and spaces, and is a valid expression
  • • `+` is never unary; `-` may be unary
  • • Every number and every running result fits in a 32-bit int
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“brackets, possibly nested”
→ The last bracket opened is the first one closed → stack.
“only + and −”
→ Inside one level, a running `result` and `sign` are enough — no terms to keep open.
“unary minus, `-(2 + 3)`”
→ A sign can apply to a whole group → save the sign together with the outer result.
03

Approach Complexity Ladder

Brute Force — evaluate the innermost bracket, paste it backO(N²)O(N)

Find an innermost (…), evaluate it flat, splice its value into the string, repeat

Better — recursion for each bracketO(N)O(N)

A function evaluates until ')'; on '(' it calls itself for the group

Optimal — explicit stack of (result, sign)O(N)O(N)

On '(' push (result, sign) and reset; on ')' result = saved + savedSign · result

Brute Force Intuition

The first ) in the string and the last ( before it form an innermost bracket: nothing inside it has brackets. Evaluate that flat + - expression, replace the whole ( … ) with the resulting number, and repeat. When no brackets are left, evaluate the flat string.

Recognition rule for this sub-pattern:

Operands wait on a stack until their operator can be applied. Parentheses start a fresh level.
Remember this · Stack
Trigger
Matching pairs, nested structure, undo, evaluating expressions, "the last one wins".
Think
Does each new element need to look at the most recent unresolved element?
Key question
What does an item on the stack represent, and when does it get resolved?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.