2Brute2Best
🔥0

Basic Calculator II

Medium·LeetCode #227 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a string s that holds a normal arithmetic expression, like "3+2*2". Return its value.

  • The expression has non-negative integers and the operators +, -, *, /. There are no brackets.
  • * and / are done before + and - (normal school rules). Operators of the same strength go left to right.
  • Integer division truncates toward zero.
  • There may be spaces anywhere between numbers and operators.
  • 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 = "3+2*2"
Output: 7

2*2 is done first and gives 4, then 3 + 4 = 7.

Example 2
Input: s = " 3/2 "
Output: 1

3 / 2 = 1.5, which is cut to 1. The spaces are ignored.

Example 3
Input: s = " 3+5 / 2 "
Output: 5

5 / 2 is done first and gives 2, then 3 + 2 = 5.

Constraints
  • • 1 ≤ s.length ≤ 3 · 10^5
  • • `s` contains only digits, `+ - * /` and spaces
  • • `s` is a valid expression, and every number in it is in [0, 2^31 − 1]
  • • The final answer fits in a 32-bit int
02

Pattern Recognition Clues

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

“* and / before + and −”
→ Precedence → a number can't be added in until you know no `*` or `/` follows it. Keep it open on a stack.
“no brackets”
→ Only two levels of strength → the stack only needs to hold terms, and only the top is ever open.
“spaces anywhere”
→ Read numbers digit by digit and act only on real operators.
03

Approach Complexity Ladder

Brute Force — collapse * and / one at a timeO(N²)O(N)

Split into tokens, repeatedly collapse the first * or /, then add up

Better — stack of signed termsO(N)O(N)

+ pushes n, − pushes −n, * and / change the top; answer = sum of stack

Optimal — result + last (no stack)O(N)O(1)

Keep the sum of finished terms and the latest term only

Brute Force Intuition

Do what you were taught in school. First split the string into numbers and operators. Then find the first * or /, compute it, and replace the three tokens by the result. Repeat until no * or / is left. Now only + and - remain, and those can be done left to right.

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.