Skip to the code
cf.
+25 XP

#267 Smallest Code After Dropping Digits

medium

A coupon system shortens numeric codes by deleting exactly k digits, and it wants the resulting number to be as small as possible. The remaining digits keep their order.

Write dropDigits(code, k) where code is a string of digits. Return the smallest possible result as a string without leading zeros. If nothing is left, or only zeros are left, return "0".

Example 1

Input: code = "1432219", k = 3
Output: "1219"

Dropping 4, 3, and one 2 leaves 1219, the smallest choice.

Example 2

Input: code = "10200", k = 1
Output: "200"

Dropping 1 leaves 0200, which is 200 without leading zeros.

Constraints

  • 1 <= k <= code.length <= 10^5
  • code has only digits and no leading zeros, except "0" itself.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).