605. Remove K Digits to Get Smallest Number

EasyStackMonotonic StackGreedyString

Given a non-negative integer as a string num and an integer k, remove exactly k digits so the resulting number is the smallest possible. Return it as a string without leading zeros (or "0" if empty). The input is JSON {num, k}.

Input: JSON {num, k}.

Output: String — the smallest resulting number.

Examples

Example 1
Input: {"num":"1432219","k":3}
Output: 1219
Explanation: Remove 4,3,2 to get 1219.
Example 2
Input: {"num":"10200","k":1}
Output: 200
Explanation: Remove 1, drop the leading zero.
Example 3
Input: {"num":"10","k":2}
Output: 0
Explanation: Removing all leaves 0.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →