class Solution:
def thousandSeparator(self, n: int) -> str:
s = str(n)
res = []
count = 0
for ch in reversed(s):
if count and count % 3 == 0:
res.append('.')
res.append(ch)
count += 1
return ''.join(reversed(res))
class Solution {
public:
string thousandSeparator(int n) {
string s = to_string(n);
string res = "";
int count = 0;
for (int i = s.size() - 1; i >= 0; --i) {
if (count && count % 3 == 0) res = '.' + res;
res = s[i] + res;
count++;
}
return res;
}
};
class Solution {
public String thousandSeparator(int n) {
String s = Integer.toString(n);
StringBuilder res = new StringBuilder();
int count = 0;
for (int i = s.length() - 1; i >= 0; i--) {
if (count > 0 && count % 3 == 0) {
res.append('.');
}
res.append(s.charAt(i));
count++;
}
return res.reverse().toString();
}
}
var thousandSeparator = function(n) {
let s = n.toString();
let res = [];
let count = 0;
for (let i = s.length - 1; i >= 0; i--) {
if (count > 0 && count % 3 === 0) res.push('.');
res.push(s[i]);
count++;
}
return res.reverse().join('');
};
The "Thousand Separator" problem asks you to format a given integer n
into a string representation where every three digits are separated by a dot ('.'
), starting from the right. For example, if n = 123456789
, the expected output is "123.456.789"
. If the number has less than four digits, it should be returned as-is, without any separator.
Key constraints:
n
is 0, the output is "0"
.The problem is essentially about string formatting: grouping digits in sets of three from the right, and inserting a dot between groups. The naive (brute-force) approach would be to convert the number to a string, then insert separators by counting digits from the right.
The main challenge is to handle the grouping correctly, especially for numbers whose length is not a multiple of three. We also need to avoid unnecessary separators at the start or end of the string.
An optimized approach is to iterate through the string representation of n
from right to left, appending a dot after every three digits (except at the end), and then reverse the result to get the correct order.
n
to its string representation.
This approach is efficient because it processes each digit exactly once and only needs a single pass through the string.
Let's take n = 123456789
as an example:
"123456789"
"123.456.789"
For a smaller number, like n = 123
:
"123"
"123"
Brute-force Approach:
A brute-force approach might involve repeatedly slicing the string and concatenating with separators, which could take O(k^2) time where k is the number of digits (due to repeated string concatenation).
Optimized Approach:
The optimized solution processes each digit exactly once, so the time complexity is O(k), where k is the number of digits in n
. Space complexity is also O(k), since we store the formatted string.
The "Thousand Separator" problem is a string manipulation task that can be solved efficiently by iterating through the number's digits from right to left, inserting dots after every three digits, and then reversing the result. This approach is both simple and efficient, requiring only a single pass and minimal extra space. It demonstrates careful handling of grouping and formatting, which is a common pattern in software development.