Remove Trailing Zeros From a String

EASY

Description

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

Checkout 2 different approaches to solve Remove Trailing Zeros From a String. Click on different approaches to view the approach and algorithm in detail.

Regular Expression Replacement

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.

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 Analysis

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

Pros and Cons

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.

Code Solutions

Checking out 3 solutions in different languages for Remove Trailing Zeros From a String. Click on different languages to view the code.

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 Solution

Watch the video walkthrough for Remove Trailing Zeros From a String



Data Structures:

String

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.