Thousand Separator
EasyPrompt
Given an integer n, add a dot (".") as the thousands separator and return it in string format.
Example 1:
Input: n = 987
Output: "987"Example 2:
Input: n = 1234
Output: "1.234"
Constraints:
0 <= n <= 231 - 1
Approaches
2 approaches with complexity analysis and trade-offs.
This is a very intuitive approach. We first convert the number to a string. Then, we iterate through this string from right to left, building a new string. We keep a counter to track digits, and every three digits, we add a dot. Since we build the result string in reverse order (from the least significant digit to the most significant), we need to reverse it at the end to get the final correct format.
Algorithm
- Convert the input integer
nto its string representation, let's call its. - If the length of
sis less than or equal to 3, no separator is needed. Returnsdirectly. - Initialize a
StringBuilderto construct the result. - Initialize a counter,
digitCount, to 0. - Loop through the string
sfrom the last character to the first (indexs.length() - 1down to0). - Inside the loop, append the current character to the
StringBuilder. - Increment
digitCount. - Check if
digitCountis 3 and if we are not at the very beginning of the string (i.e., the loop index is not 0). If both conditions are true, append a.separator to theStringBuilderand resetdigitCountto 0. - After the loop completes, the
StringBuilderholds the formatted string but in reverse order. - Call the
reverse()method on theStringBuilderand then convert it to a string to get the final answer.
Walkthrough
This approach works by processing the number's string representation from right to left, which is the natural way to group digits into thousands. A StringBuilder is used for efficient string construction.
class Solution { public String thousandSeparator(int n) { String s = Integer.toString(n); if (s.length() <= 3) { return s; } StringBuilder resultBuilder = new StringBuilder(); int count = 0; for (int i = s.length() - 1; i >= 0; i--) { resultBuilder.append(s.charAt(i)); count++; // Add a dot after every 3 digits, but not at the very beginning if (count % 3 == 0 && i > 0) { resultBuilder.append('.'); } } // The string was built backwards, so we need to reverse it return resultBuilder.reverse().toString(); }}Complexity
Time
O(L), where L is the number of digits in `n`. Converting the integer to a string takes O(L). The loop iterates L times, and operations inside (appending to `StringBuilder`) are amortized O(1). The final reversal also takes O(L).
Space
O(L), where L is the number of digits in `n`. The `StringBuilder` requires space proportional to the number of digits plus the separators.
Trade-offs
Pros
The logic is simple and follows a natural way of thinking about the problem (grouping from the right).
Easy to implement correctly.
Cons
Requires a final reversal step, which adds an extra pass over the constructed string.
Solutions
Solution
class Solution {public String thousandSeparator(int n) { int cnt = 0; StringBuilder ans = new StringBuilder(); while (true) { int v = n % 10; n /= 10; ans.append(v); ++cnt; if (n == 0) { break; } if (cnt == 3) { ans.append('.'); cnt = 0; } } return ans.reverse().toString(); }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.