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.
This approach involves reversing the string so that trailing zeros become leading zeros. Then, remove those leading zeros until reaching a non-zero character, and finally reverse the result back.
This method is straightforward due to simplicity in string manipulation when leading operations are involved.
In the given C solution, we iterate backward through the string 'num'. For each zero at the end, we replace it with a null character until a non-zero digit is found.
Time Complexity: O(n), where n is the length of the string, as we may have to traverse the entire string.
Space Complexity: O(1), as we modify the string in place without using extra space.
This approach discards trailing zeros by counting them from the end until a non-zero character appears, leading to a substring from the beginning up to this position.
By studying C's direct null character placement, iterative decrementing ensures reaching the first non-zero digit, then sets the string end.
Time Complexity: O(n), since we check each character in reverse.
Space Complexity: O(1), with in-place modification.
We can traverse the string from the end to the beginning, stopping when we encounter the first character that is not 0. Then, we return the substring from the beginning to this character.
The time complexity is O(n), where n is the length of the string. Ignoring the space consumed by the answer string, the space complexity is O(1).
| Approach | Complexity |
|---|---|
| Reverse and Trim Approach | Time Complexity: O(n), where n is the length of the string, as we may have to traverse the entire string. |
| Iterative from Back to Front | Time Complexity: O(n), since we check each character in reverse. |
| Traversal | — |
| 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 |
Remove Trailing Zeros From a String - Leetcode 2710 • Algorithms Casts • 866 views views
Watch 9 more video solutions →Practice Remove Trailing Zeros From a String with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor