Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

94. Remove K Digits

MediumDay 27 of 60Stack & Queue

You are given a string num holding a non-negative integer and an integer k. Remove exactly k digits so that the remaining number is as small as possible, and return it as a string. Remove any leading zeros from the result, and return "0" if nothing is left.

Example 1
Input: num = "1432219", k = 3
Output: "1219"
Remove 4, 3 and the first 2.
Example 2
Input: num = "10200", k = 1
Output: "200"
Removing the 1 leaves "0200", which becomes "200".
Constraints
  • 1 <= k <= num.length <= 10^5
  • `num` has only digits
  • `num` has no leading zeros except for the number zero itself
Hints
Frequently asked at: Amazon, Microsoft, Google, Paytm
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min