402. Remove K Digits
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 35% Topics: String, Stack, Greedy, Monotonic Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
result = []
for d in num:
while k and result and result[-1] > d:
result.pop()
k -= 1
result.append(d)
return ''.join(result).lstrip('0')[:-k or None] or '0'
Solution from kamyu104/LeetCode-Solutions · MIT