Watch 10 video solutions for Remove Trailing Zeros From a String, a easy level problem involving String. This walkthrough by Algorithms Casts has 866 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 1000num consists of only digits.num doesn't have any leading zeros.Problem Overview: You receive a numeric string and must return the same number after removing all trailing '0' characters. Only zeros at the end should be removed. Zeros that appear in the middle or beginning must remain unchanged.
This is a classic string processing task. The core idea is identifying the last non-zero character and returning the substring up to that position.
Approach 1: Reverse and Trim Approach (O(n) time, O(n) space)
This approach reverses the string so trailing zeros become leading zeros. Once reversed, you iterate from the start and skip all leading '0' characters. After locating the first non-zero digit, take the remaining substring and reverse it again to restore the original order.
The key idea is transforming the problem: removing trailing characters becomes removing leading characters after reversal. This technique is common in string manipulation problems where operations at the end of the string are easier from the front.
The downside is extra memory usage. Reversing the string creates intermediate strings, which increases space complexity to O(n). The time complexity is still O(n) because each character is processed a constant number of times.
Approach 2: Iterative from Back to Front (O(n) time, O(1) space)
This is the most direct solution. Start from the last character of the string and move backward while the current character equals '0'. Stop when you encounter the first non-zero digit or reach the beginning of the string.
The index where this scan stops marks the last digit that should remain. Return the substring from the start of the string up to that index (inclusive). This avoids reversing or creating unnecessary intermediate strings.
This method effectively simulates a two‑pointer style scan from the end, though only one pointer is needed. The string is traversed at most once, giving O(n) time complexity. Since only a few variables are used, the extra space remains O(1).
This pattern appears frequently in string and array problems where irrelevant elements accumulate at the end. Scanning backward is often simpler than restructuring the entire sequence.
Recommended for interviews: The Iterative from Back to Front approach is what interviewers expect. It shows you recognize the simplest possible traversal and avoid unnecessary operations like reversing the string. Mentioning the reverse approach first demonstrates understanding of alternative transformations, but the backward scan highlights strong problem‑solving instincts and optimal space usage.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Reverse and Trim Approach | O(n) | O(n) | When using built-in reverse operations or when handling leading-character trimming patterns |
| Iterative from Back to Front | O(n) | O(1) | Best general solution; minimal memory and straightforward logic |