Remove K Digits

Medium Topics Company Tags

The decimal string num represents a nonnegative integer. Delete exactly k digits without changing the order of the digits that remain, and return the smallest possible resulting integer as a string.

Remove leading zeros from the result. If no digits remain, or all remaining digits are zero, return "0".

Example 1:

Input: num = "35241"
k = 2

Output: "241"

Explanation: Deleting 5 and 4 leaves "321", but deleting 3 and 5 leaves the smaller "241". Among all ways to keep three digits in order, "241" is the smallest.

Example 2:

Input: num = "1002"
k = 1

Output: "2"

Explanation: Delete the leading 1 to leave "002". Removing the leading zeros gives the output "2".

Example 3:

Input: num = "7"
k = 1

Output: "0"

Explanation: Deleting the only digit leaves no digits, which is represented by "0".

Constraints:

  • 1 <= k <= num.length <= 100000
  • num contains only decimal digits.
  • num has no leading zeros unless it is exactly "0".


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.