Talentd
FleetCode

2710. Remove Trailing Zeros From a String

Easy79.5% acceptance

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.

Topics

Hints open one at a time

Hint 1
Find the last non-zero digit in num.
4:15
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def removeTrailingZeros(self, num: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"51230100"
Input
"123"
Your code is saved on this device.Solution