Thousand Separator
EASYDescription
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
Checkout 2 different approaches to solve Thousand Separator. Click on different approaches to view the approach and algorithm in detail.
Iterate from Right with StringBuilder and Reverse
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.
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 Analysis
Pros and Cons
- The logic is simple and follows a natural way of thinking about the problem (grouping from the right).
- Easy to implement correctly.
- Requires a final reversal step, which adds an extra pass over the constructed string.
Code Solutions
Checking out 3 solutions in different languages for Thousand Separator. Click on different languages to view the code.
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 Solution
Watch the video walkthrough for Thousand Separator
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.