Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions String/2710. Remove Trailing Zeros From a String.py

This file was deleted.

Empty file.
29 changes: 29 additions & 0 deletions problems/easy/remove_trailing_zeros_from_a_string_2710/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Tags: String


class Solution:
def remove_trailing_zeros(self, num: str) -> str:
"""
Remove trailing zeros from a string representation of a positive integer.

Args:
num: A string representation of a positive integer without leading zeros

Returns:
The string representation of the integer without trailing zeros

Time complexity: O(n) where n is the length of the input string
- Worst case: O(n) when iterating through the entire string (e.g., "10000", "5")
- Best case: O(1) when the last character is already non-zero
- The loop traverses from the end until finding the first non-zero digit

Space complexity: O(n) for the result string, O(1) auxiliary space
- O(n) for the result string which is required output
- O(1) additional space for the 'res' variable and loop counter
"""
res = ""
for i in range(len(num) - 1, -1, -1):
if not res and num[i] != "0":
res = num[: i + 1]
break
return res
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from tests.base_test import BaseTestSolution
from .solution import Solution


class TestSolution(BaseTestSolution):
solution = Solution()

@pytest.mark.parametrize("method_name, num, expected, timeout", [
('remove_trailing_zeros', "51230000", "5123", None),
('remove_trailing_zeros', "123", "123", None),
('remove_trailing_zeros', "9", "9", None),
])
def test_remove_trailing_zeros(self, method_name, num, expected, timeout):
self._run_test(self.solution, method_name, (num,), expected, timeout)
Loading