Skip to main content
Back to problems
Leetcode
Medium
Stacks
Strings
Greedy
Google
Amazon
Remove K Digits

Remove exactly k digits from a non-negative integer so the remaining number is as small as possible.

Acceptance 0%
Problem Statement

Given a non-negative integer represented as a string num and an integer k, remove exactly k digits from the string so that the resulting number is the smallest possible.

Return the smallest possible number as a string. The result should not contain unnecessary leading zeros. If all digits are removed or the number becomes empty after trimming, return "0".

The relative order of the remaining digits must stay the same.

Input Format

  • A string num representing a non-negative integer.
  • An integer k, the number of digits to remove.

Output Format

  • Return a string representing the smallest possible number after removing exactly k digits.

Constraints

  • 1 <= num.length
  • 0 <= k <= num.length
  • num contains only digits 0-9
  • The answer must preserve the relative order of the remaining digits
Examples
Sample cases returned by the problem API.

Example 1

Input

num = "1432219", k = 3

Output

"1219"

Explanation

Remove the digits 4, 3, and 2 to make the smallest possible number while preserving order.

Example 2

Input

num = "10200", k = 1

Output

"200"

Explanation

Removing the leading 1 gives "0200", which is normalized to "200".

Show 1 more example

Example 3

Input

num = "10", k = 2

Output

"0"

Explanation

All digits are removed, so the result is "0".

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.