मुख्य कंटेंट तक स्किप करें
Back to ChallengesRemove K DigitsHard 45 min

Remove K Digits

Given string num representing a non-negative integer num, and an integer k, return the smallest possible integer after removing k digits from num.

Examples

Input: num = '1432219', k = 3
Output: '1219'
Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

Constraints

  • 1 <= k <= num.length <= 10^5
  • num consists of only digits.
  • num does not have any leading zeros except for the zero itself.

Complexity Analysis

Time
O(N)
each digit is pushed/popped at most once.
Space
O(N)
using a stack.

Test Cases

#1 Standard case
Input: num = '1432219', k = 3
Expected: '1219'
#2 Remove to handle leading zeros
Input: num = '10200', k = 1
Expected: '200'
Output
Click "Run Code" to see output here...