FastPrepLargest Number After Removing K Digits

Largest Number After Removing K Digits

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

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) → String

Examples

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.
  • num contains only decimal digits.
  • 0 <= k <= num.length.

More Google problems

See Google hiring insights
public String largestAfterRemovingKDigits(String num, int k) {
    // write your code here
}
num"4902"
k2
expected"92"
Checking account…