Largest Number After Removing K Digits
Problem statement
Given a decimal string num and an integer k, remove exactly k digits while preserving the relative order of the remaining digits.
Return the largest possible remaining integer as a string. Remove leading zeroes from the result. If no digit remains or the result contains only zeroes, return "0".
Function
largestAfterRemovingKDigits(num: String, k: int) → StringExamples
Example 1
num = "4902"k = 2return = "92"Removing 4 and 0 leaves the largest order-preserving two-digit value.
Example 2
num = "1432219"k = 3return = "4329"The greedy removals preserve the subsequence 4329.
Example 3
num = "10"k = 2return = "0"Every digit is removed, so the canonical result is zero.
Constraints
1 <= num.length <= 200000.numcontains only decimal digits.0 <= k <= num.length.