Class Solution

java.lang.Object
g0401_0500.s0402_remove_k_digits.Solution

public class Solution extends Object
402 - Remove K Digits.<p>Medium</p> <p>Given string num representing a non-negative integer <code>num</code>, and an integer <code>k</code>, return <em>the smallest possible integer after removing</em> <code>k</code> <em>digits from</em> <code>num</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> num = &ldquo;1432219&rdquo;, k = 3</p> <p><strong>Output:</strong> &ldquo;1219&rdquo;</p> <p><strong>Explanation:</strong> Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> num = &ldquo;10200&rdquo;, k = 1</p> <p><strong>Output:</strong> &ldquo;200&rdquo;</p> <p><strong>Explanation:</strong> Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> num = &ldquo;10&rdquo;, k = 2</p> <p><strong>Output:</strong> &ldquo;0&rdquo;</p> <p><strong>Explanation:</strong> Remove all the digits from the number and it is left with nothing which is 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= k <= num.length <= 10<sup>5</sup></code></li> <li><code>num</code> consists of only digits.</li> <li><code>num</code> does not have any leading zeros except for the zero itself.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • removeKdigits

      public String removeKdigits(String num, int k)