Teaching Kids Programming – Remove Trailing Zeros From a String (strip, lstrip, rstrip)


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.

Example 1:
Input: num = “51230100”
Output: “512301”
Explanation: Integer “51230100” has 2 trailing zeros, we remove them and return integer “512301”.

Example 2:
Input: num = “123”
Output: “123”
Explanation: Integer “123” has no trailing zeros, we return integer “123”.

Constraints:
1 <= num.length <= 1000
num consists of only digits.
num doesn’t have any leading zeros.

Remove Trailing Zeros From a String (strip, lstrip, rstrip)

A string in Python is immutable – we cannot change it. However, we can convert it to list, remove the trailing zeros and convert it back to string using the join function.

We repeat checking the rightmost character to see if it is zero, use the pop to remove the last character. The time complexity is O(N) in the worst cases, all characters are removed. The space complexity is O(N) as we need to store a copy of the immutable string as a mutable list/array.

1
2
3
4
5
6
7
8
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        def f(x, t):
            x = list(x)
            while x and x[-1] == t:
                x.pop()
            return "".join(x)
        return f(num, '0')
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        def f(x, t):
            x = list(x)
            while x and x[-1] == t:
                x.pop()
            return "".join(x)
        return f(num, '0')

If we want to remove the leading zeros, we might need to use a deque (double-ended queue) to achieve O(1) constant time to push/pop on both sides. The above code takes O(N) time. We can change to the following using deque:

1
2
3
4
5
6
7
8
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        def f(x, t):
            x = deque(list(x))
            while x and x[-1] == t:
                x.pop()
            return "".join(x)
        return f(num, '0')
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        def f(x, t):
            x = deque(list(x))
            while x and x[-1] == t:
                x.pop()
            return "".join(x)
        return f(num, '0')

If we want to remove the leading characters (strip left):

1
2
3
4
5
def lstrip(x, t):
    x = deque(list(x))
    while x and x[0] == t:
        x.popleft()
    return "".join(x)
def lstrip(x, t):
    x = deque(list(x))
    while x and x[0] == t:
        x.popleft()
    return "".join(x)

In Python, we can use lstrip and rstrip to remove the leading and trailing characters respectively. If the characters is not specified, it is white space.

1
2
3
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        return num.rstrip('0')
class Solution:
    def removeTrailingZeros(self, num: str) -> str:
        return num.rstrip('0')

The strip function removes both leading and trailing so it is the same as:

1
2
def strip(s, x):
    return s.lstrip(x).rstrip(x)
def strip(s, x):
    return s.lstrip(x).rstrip(x)

strip = lstrip + rstrip

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
594 words
Last Post: Teaching Kids Programming - Algorithms to Find Two Smallest Numbers (Buy Two Chocolates - Heap - Sorting - Linear Scan)
Next Post: Teaching Kids Programming - Introduction to Units of Information (Byte, KB, MB, GB, TB, PB)

The Permanent URL is: Teaching Kids Programming – Remove Trailing Zeros From a String (strip, lstrip, rstrip)

Leave a Reply