bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 402. Remove K Digits

402. Remove K Digits

Leetcode Solutions

Greedy with Stack

  1. Initialize an empty stack to store the digits of the final number.
  2. Iterate over each digit in the input number.
    • If the current digit is smaller than the top of the stack and we still need to remove more digits, pop the stack.
    • Push the current digit onto the stack.
  3. If we have not removed k digits after the iteration, remove the remaining digits from the end of the stack.
  4. Convert the stack to a string and remove any leading zeros.
  5. If the resulting string is empty, return '0'. Otherwise, return the string.
UML Thumbnail

Brute-force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...