Problem Description
Given a non-negative integer n, convert it to a string and insert a dot “.” as the thousands separator. For example, 1234 becomes "1.234" and 987 remains "987".
Key Insights
- Convert the integer to its string representation.
- Process the string from right to left to group digits in sets of three.
- Insert a dot between these groups.
- Reverse the result to restore the original digit order.
Space and Time Complexity
Time Complexity: O(n) where n is the number of digits
Space Complexity: O(n) for storing the new string with separators
Solution
We first convert the given number to a string. Then, starting from the end of the string, we group the digits in batches of three. For each complete group (except possibly the first), a dot is added as a separator. Finally, we reverse the built string to obtain the thousands-separated number in correct order. This approach efficiently handles the grouping without converting back and forth from numeric types.