Thousand Separator - Solution & Explanation
Problem Statement
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
Approach Overview
Problem Overview: You receive a non‑negative integer n. The task is to convert it into a string where digits are grouped by thousands using a dot (.) as the separator. For example, 1234567 becomes 1.234.567. The challenge is purely formatting, but you must place separators exactly every three digits from the right side.
Approach 1: String Processing from Right to Left (O(n) time, O(n) space)
Convert the integer to a string and iterate from the last digit toward the first. Maintain a counter and insert a dot after every three digits while building the result string. Many implementations push characters into a buffer (or StringBuilder) and reverse the result at the end. The key insight: thousand separators are determined relative to the rightmost digit, so right‑to‑left iteration avoids complicated index math. This approach relies only on basic string manipulation and works consistently across languages.
The algorithm scans the digits once, making the time complexity O(n), where n is the number of digits. The output string requires O(n) extra space because separators are inserted during construction. This is the most common implementation in coding interviews because it is straightforward and language‑agnostic.
Approach 2: Using Regular Expression Formatting (O(n) time, O(n) space)
Languages with strong regular expression support can solve this with pattern-based formatting. First convert the number to a string, then apply a regex that inserts a dot before groups of three digits from the right side. Patterns typically use lookahead expressions such as (?=(\d{3})+$) to match positions where a separator should appear.
The regex engine scans the string and inserts separators automatically, producing clean and concise code. Complexity remains O(n) because the regex engine processes each digit once in typical implementations. Space complexity is also O(n) due to the newly constructed formatted string. This method is convenient in languages like Python, Java, and JavaScript where regex replacements are expressive and concise.
Recommended for interviews: The right‑to‑left string processing approach is usually expected. It shows you understand digit grouping logic and basic string processing operations without relying on language‑specific regex tricks. Regex solutions are perfectly valid in production code, but interviewers often prefer the explicit iteration because it demonstrates clear reasoning about indexing and formatting rules.
Approach 1: String Processing from Right to Left
This approach involves converting the number to a string, and then processing it from the rightmost digit to the leftmost. By counting every three digits, we insert a dot (".") as a separator. This helps in formatting the number correctly especially when its length is greater than 3.
This C solution uses a buffer `temp` to hold the string representation of the number `n`. It iterates through this string in reverse, inserting a dot after every three characters. The final result is then reversed to provide the correct numeral.
Complexity
Time Complexity: O(log n), since it involves iterating through the digits of the number.
Space Complexity: O(log n), for storing the number's string representation.
Approach 2: Using Regular Expression Formatting
This approach makes use of string manipulation libraries available in some languages to directly format numbers with a thousands separator using regular expressions or language-specific numeral formatting techniques.
In Java, the `DecimalFormat` class assists in formatting numbers with thousands separator. Here, we specify a comma (,) as the separator and then replace it with a dot (.) for the output.
Code
Java
Python
C#
JavaScript
Complexity
Time Complexity: O(1) average due to internal library optimizations.
Space Complexity: O(1), only additional space for formatted string output.
Approach 3: Default Approach
Try this approach in the editor →Complexity Comparison
| Approach | Complexity |
|---|---|
| String Processing from Right to Left | Time Complexity: O(log n), since it involves iterating through the digits of the number. |
| Using Regular Expression Formatting | Time Complexity: O(1) average due to internal library optimizations. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Processing from Right to Left | O(n) | O(n) | Best general solution; simple logic and works in any language |
| Regular Expression Formatting | O(n) | O(n) | Concise implementation when strong regex support is available |
Video Solution
LeetCode in Python 1556. Thousand Separator - Michelle小梦想家 • Michelle小梦想家 • 1,968 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Thousand Separator easy or hard?
Thousand Separator Python/Java solution
How to solve Thousand Separator in O(n)?
What is the best approach for Thousand Separator?
Is Thousand Separator asked at Google/Amazon/Meta?
What data structure is used in Thousand Separator?
What is the time complexity of Thousand Separator?
Ready to solve this problem?
Practice Thousand Separator with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor