FastPrepRemove K Digits
Problem · Stack

Remove K Digits

Learn this problem
MediumThe D. E. Shaw Group logoThe D. E. Shaw GroupFULLTIMEONSITE INTERVIEW

Problem statement

You are given a string num that represents a non-negative integer, and an integer k.

Remove exactly k digits from num so the remaining digits stay in their original relative order and form the smallest possible integer.

Return that integer as a string. Do not keep leading zeros, except for the integer 0 itself.

Function

removeKdigits(num: String, k: int) → String

Examples

Example 1

num = "1432219"k = 3return = "1219"

Removing the digits 4, 3, and 2 from 1432219 leaves 1219, which is the smallest remaining integer.

Example 2

num = "10200"k = 1return = "200"

Removing the leading 1 leaves 0200, which becomes 200 after leading zeros are stripped.

Example 3

num = "10"k = 2return = "0"

Every digit is removed, so the result is 0.

Constraints

  • 1 <= num.length <= 10^5.
  • 1 <= k <= num.length.
  • num consists of digits only.
  • num has no leading zeros except when num is "0".

More The D. E. Shaw Group problems

drafts saved locally
public String removeKdigits(String num, int k) {
  // Write your code here.
}
num"1432219"
k3
expected"1219"
checking account