#K66692. Remove K Digits
Remove K Digits
Remove K Digits
Problem Statement:
You are given a non-negative integer represented as a string and an integer \( k \). Your task is to remove exactly \( k \) digits from the number so that the resulting number is the smallest possible. Note that the resulting number should not contain any leading zeros, unless the number is exactly zero.
Approach: A common approach to solving this problem is to use a greedy algorithm with a stack. Iterate over the digits and remove any digit that is larger than a following digit if you still have digits left to remove. Finally, if there are still digits to remove, remove them from the end of the number.
Input: A string representing the number, and an integer \( k \) denoting the number of digits to remove.
Output: The smallest possible number (as a string) after removing exactly \( k \) digits.
inputFormat
The input is read from standard input (stdin) and consists of two lines. The first line is a non-negative integer represented as a string. The second line is an integer ( k ), representing the number of digits to remove.
outputFormat
Print the smallest possible number (as a string) after removing exactly ( k ) digits from the input number to standard output (stdout).## sample
1432219
3
1219