2Brute2Best
🔥0

Remove K Digits

Medium·LeetCode #402 Pro OnlyPattern Transfer
LeetCode
01

Problem Statement

You are given a non-negative number as a string num, and an integer k.

Remove exactly k digits from num. The other digits stay in their original order. Return the smallest number you can get, as a string.

The result should not have leading zeros. If nothing is left, or only zeros are left, return "0".

Input

A digit string num and an integer k.

Output

The smallest possible number after removing k digits, as a string without leading zeros.

Example 1
Input: num = "1432219", k = 3
Output: "1219"

Remove 4, 3 and the first 2. What is left is 1219.

Example 2
Input: num = "10200", k = 1
Output: "200"

Remove the 1 to get "0200". Leading zeros are dropped, so the answer is "200".

Example 3
Input: num = "10", k = 2
Output: "0"

Both digits are removed. Nothing is left, so the answer is "0".

Constraints
  • • 1 ≤ k ≤ num.length ≤ 10^5
  • • `num` has only digits
  • • `num` has no leading zeros, except when the number is just "0"
02

Pattern Recognition Clues

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

“remove k digits”
→ We drop items, and a new item can make earlier ones worth dropping → pop from a stack.
“smallest number”
→ Smaller digits should come first → keep the kept digits increasing: a bigger digit before a smaller one is popped.
“original order”
→ We keep a subsequence, so a left-to-right scan with a stack fits.
03

Approach Complexity Ladder

Brute Force — remove one digit at a timeO(N · K)O(N)

k times: scan for the first digit bigger than the next one and erase it

Optimal — increasing stack of kept digitsO(N)O(N)

Pop a bigger top while a smaller digit arrives (and k > 0); trim the tail; strip zeros

Brute Force Intuition

Make the k removals one by one. Each time, scan from the left for the first digit that is bigger than the digit right after it, and erase it. If the digits never go down, erase the last digit. At the end, strip leading zeros.

Recognition rule for this sub-pattern:

Need the first smaller value. Keep an increasing stack; greedy "remove bigger digits before me" problems also live here.
Remember this · Monotonic Stack
Trigger
Next/previous greater or smaller element, spans, histogram areas, "for each element, how far does it dominate?"
Think
When a new element arrives, which older elements can never be the answer again?
Key question
Is my stack increasing or decreasing, and what do I record when I pop?
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 Monotonic Stack translate into production architectures and low-level memory mechanics.