Remove Trailing Zeros From a String

Easy
#2445Time: O(N), where N is the length of the string. The regex engine needs to scan the string to find a match.Space: O(N), where N is the length of the input string. This is because `replaceAll` creates a new string to store the result.
Data structures

Prompt

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.

Approaches

2 approaches with complexity analysis and trade-offs.

A straightforward approach is to use regular expressions to identify and remove the trailing zeros. A regex pattern can be crafted to match one or more '0' characters at the very end of the string.

Algorithm

  1. Define a regular expression pattern 0+$ to match trailing zeros.
  2. Use the string's built-in replacement function to replace all occurrences of the pattern with an empty string.
  3. Return the resulting string.

Walkthrough

The core idea is to leverage the string replacement functionality available in most programming languages, combined with a regular expression. The regular expression 0+$ is used. The 0+ part matches the character '0' one or more times, and the $ is an anchor that asserts the position at the end of the string. This pattern specifically targets sequences of one or more zeros that are at the end of the string. We then use a function like replaceAll() to substitute the matched pattern (the trailing zeros) with an empty string "". If there are no trailing zeros, the pattern won't match, and the original string will be returned unchanged.

class Solution {    public String removeTrailingZeros(String num) {        // The regex "0+$" matches one or more '0's at the end of the string.        // replaceAll replaces this match with an empty string.        return num.replaceAll("0+$", "");    }}

Complexity

Time

O(N), where N is the length of the string. The regex engine needs to scan the string to find a match.

Space

O(N), where N is the length of the input string. This is because `replaceAll` creates a new string to store the result.

Trade-offs

Pros

  • Very concise and requires minimal code.

  • Highly readable for those familiar with regular expressions.

Cons

  • Can be less performant than a manual loop due to the overhead of compiling and executing the regular expression.

  • Might be less intuitive for developers not comfortable with regex.

Solutions

class Solution { public String removeTrailingZeros ( String num ) { int i = num . length () - 1 ; while ( num . charAt ( i ) == '0' ) { -- i ; } return num . substring ( 0 , i + 1 ); } }

Video walkthrough

Newsletter

One sharp idea, every week

System design and interview prep — short enough to finish.

No spam. Unsubscribe anytime.

Practice

Same difficulty — related problems to reinforce the pattern.