Skip to main content

Remove Trailing Zeros From a String - Solution & Explanation

EasyString11 min read
Practice this problem

Problem Statement

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.

Approach Overview

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 1: Reverse and Trim Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Iterative from Back to Front

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), since we check each character in reverse.
Space Complexity: O(1), with in-place modification.

Try this approach in the editor →

Approach 3: Traversal

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).

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(1), as we modify the string in place without using extra space.

Iterative from Back to Front

Time Complexity: O(n), since we check each character in reverse.
Space Complexity: O(1), with in-place modification.

Traversal—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Reverse and Trim ApproachO(n)O(n)When using built-in reverse operations or when handling leading-character trimming patterns
Iterative from Back to FrontO(n)O(1)Best general solution; minimal memory and straightforward logic

Video Solution

Remove Trailing Zeros From a String - Leetcode 2710 • Algorithms Casts • 873 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Remove Trailing Zeros From a String easy or hard?
Remove Trailing Zeros From a String is categorized as an Easy problem. It focuses on basic string traversal and boundary conditions rather than complex algorithms or data structures.
Remove Trailing Zeros From a String Python/Java solution
In Python or Java, iterate from the last index of the string and decrement the pointer while the character equals '0'. After the loop ends, return the substring up to the final index. The implementation is short and runs in O(n) time with constant extra space.
How to solve Remove Trailing Zeros From a String in O(n)?
Start from the last character of the string and move backward while characters equal '0'. When the first non-zero digit appears, stop the scan and return the substring from index 0 to that position. Each character is checked at most once, giving O(n) time complexity.
What is the best approach for Remove Trailing Zeros From a String?
The best approach is scanning the string from the end until the first non-zero character appears. Once found, return the substring up to that index. This solution runs in O(n) time and uses O(1) extra space, making it both simple and optimal.
Is Remove Trailing Zeros From a String asked at Google/Amazon/Meta?
String manipulation problems like this appear frequently in coding interviews at companies such as Amazon, Google, and Meta. While the exact problem may vary, identifying and trimming characters from the end of a string is a common interview pattern.
What data structure is used in Remove Trailing Zeros From a String?
The problem primarily uses basic string operations. The algorithm relies on index traversal of a string rather than specialized data structures, though the logic resembles a backward pointer scan commonly used in two-pointer techniques.
What is the time complexity of Remove Trailing Zeros From a String?
The optimal algorithm runs in O(n) time, where n is the length of the string. In the worst case you scan the entire string when it contains only zeros or when no trailing zeros exist. Space complexity can be O(1) if you only track an index while scanning backward.

Ready to solve this problem?

Practice Remove Trailing Zeros From a String with our built-in code editor and test cases.

Practice on FleetCode