Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Remove K Digits

Medium
stackmonotonic-stackgreedystring

Given a string num representing a non-negative integer and an integer k, remove exactly k digits so the remaining number is the smallest possible. Strip leading zeros; if nothing remains, return 0.

Input

  • Line 1: the digit string num
  • Line 2: integer k

Output: the smallest resulting number as a string.

Example 1
Input:
1432219
3
Output:
1219

Remove 4, 3, 2.

Example 2
Input:
10200
1
Output:
200

Remove the 1; leading zeros are stripped.

Example 3
Input:
10
2
Output:
0

Constraints

  • 1 ≤ num.length ≤ 10^5
  • 0 ≤ k ≤ num.length
  • No leading zeros in the input (except "0" itself).
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.