Remove K Digits

Given a string nums of decimal digits and an integer k, remove exactly k digits without changing the relative order of those kept. Return the smallest…

Given a string `nums` of decimal digits and an integer `k`, remove exactly `k` digits without changing the relative order of those kept. Return the smallest possible remaining number as a string. Return its canonical decimal form: remove leading zeros, and return `"0"` if no nonzero digit remains. Formatting leading zeros does not use additional removals.

- `1 <= k <= nums.length <= 10000` - `nums` contains only digits `0` through `9`. - There is no leading zero unless `nums` is exactly `"0"`. - Return a string; do not convert the entire input into a fixed-size numeric type.

Example 1: input nums = "541892", k = 2; output "1892". Remove 5 and 4, preserving the order of 1892.

Example 2: input nums = "1002991", k = 3; output "21". Remove the first 1 and the two 9s, leaving 0021; formatting yields 21.

Example 3: input nums = "0", k = 1; output "0". Removing the sole digit leaves the canonical string "0".

Category: Stack. Topic: stack. Difficulty: Medium. Patterns: stack, greedy.