You are given a string num
representing a large integer. Return the string after removing all trailing zeros.
Input: num = "51230100"
Output: "512301"
Explanation: We remove the trailing zeros and return "512301".
Input: num = "123"
Output: "123"
Explanation: There are no trailing zeros, so we return "123".
""
since all zeros are considered trailing.rstrip
method which removes all instances of the specified characters from the end of the string. By specifying the character '0'
, we can remove all trailing zeros efficiently.def removeTrailingZeros(num: str) -> str:
return num.rstrip('0')
rstrip
function possibly iterates through the entire string once to remove the trailing zeros where n
is the length of the string.Implementing this function in an actual interview setting should be effective given the simplicity of Python’s string operations for this type of task.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?