LeetCode Problem Workspace
Thousand Separator
Convert a given integer into a string with dots as thousand separators using a precise string-driven strategy.
1
Topics
4
Code langs
3
Related
Practice Focus
Easy · String-driven solution strategy
Answer-first summary
Convert a given integer into a string with dots as thousand separators using a precise string-driven strategy.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for String-driven solution strategy
To solve Thousand Separator, immediately convert the integer to a string and scan from the end, inserting dots every three digits. This string-driven approach avoids complex arithmetic or recursion and ensures correctness for all edge cases, including small numbers. Using this method, you systematically format the number without altering its value while keeping performance linear to the string length.
Problem Statement
Given a non-negative integer n, format it by inserting a dot '.' as the thousand separator. Return the resulting value as a string. The separator should appear every three digits from the right, except for the first group which may have fewer than three digits.
For example, given n = 1234567, the output should be '1.234.567'. If n is less than 1000, no separator is added. Implement a solution that primarily relies on string manipulation rather than arithmetic operations, scanning from the back of the number to ensure correct placement of separators.
Examples
Example 1
Input: n = 987
Output: "987"
Example details omitted.
Example 2
Input: n = 1234
Output: "1.234"
Example details omitted.
Constraints
- 0 <= n <= 231 - 1
Solution Approach
Convert Integer to String
Start by converting the integer n into a string. This enables scanning and insertion operations without worrying about numerical calculations, aligning with the string-driven solution pattern.
Scan and Insert Separators
Iterate over the string from right to left, inserting a dot every three characters. Keep track of the digit count to ensure separators are only added between complete groups of three, avoiding extra separators at the start.
Build the Final String
After processing, reverse the collected characters to restore the original left-to-right order. Return the joined string as the final result, which now contains the correct thousand separators.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(log n) because the number of digits in n is proportional to log(n). Space complexity is also O(log n) to store the intermediate string and reconstructed output with separators.
What Interviewers Usually Probe
- Pay attention to scanning direction; starting from the wrong end can misplace separators.
- Watch for edge cases where n < 1000, as no separators should be added.
- Confirm that the solution does not modify the numeric value, only its string representation.
Common Pitfalls or Variants
Common pitfalls
- Inserting separators from left to right without counting groups can create extra dots.
- Failing to handle numbers less than 1000 may produce incorrect output.
- Neglecting to reverse the string after inserting separators from the end leads to misaligned formatting.
Follow-up variants
- Use commas ',' instead of dots '.' for different locale formatting.
- Apply the separator pattern to floating-point numbers by splitting integer and fractional parts.
- Implement a recursive solution that formats the number by repeatedly processing blocks of three digits.
FAQ
What is the most efficient way to add thousand separators in an integer?
The most efficient approach is to convert the integer to a string and scan from the end, inserting separators every three digits.
Does Thousand Separator require handling negative numbers?
No, the problem only specifies non-negative integers, so negative numbers do not need to be considered.
Can this pattern be applied to numbers less than 1000?
Yes, but numbers less than 1000 remain unchanged, as no separator is needed.
Why is scanning from the back recommended?
Scanning from the back aligns with the string-driven strategy and ensures dots are correctly placed between groups of three digits without extra calculations.
Are there alternative ways to solve Thousand Separator?
Alternatives include using built-in locale formatting functions or slicing the string in reverse, but the string-driven manual scan ensures full control and predictable placement.
Solution
Solution 1
#### Python3
class Solution:
def thousandSeparator(self, n: int) -> str:
cnt = 0
ans = []
while 1:
n, v = divmod(n, 10)
ans.append(str(v))
cnt += 1
if n == 0:
break
if cnt == 3:
ans.append('.')
cnt = 0
return ''.join(ans[::-1])Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
String-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward