Skip to main content

Thousand Separator - Solution & Explanation

EasyString9 min read
Practice this problem

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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

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.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
String Processing from Right to Left

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.

Using Regular Expression Formatting

Time Complexity: O(1) average due to internal library optimizations.
Space Complexity: O(1), only additional space for formatted string output.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
String Processing from Right to LeftO(n)O(n)Best general solution; simple logic and works in any language
Regular Expression FormattingO(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 is classified as an Easy problem. The challenge focuses on careful string formatting and handling digit grouping correctly rather than complex algorithms or data structures.
Thousand Separator Python/Java solution
Both Python and Java solutions typically convert the integer to a string and iterate from right to left, inserting a dot after every three digits. Python can also use regex replacement, while Java often uses StringBuilder for efficient string construction.
How to solve Thousand Separator in O(n)?
Convert the integer to a string, iterate from the last character to the first, and insert a dot after every third digit while constructing the result. Using a buffer or StringBuilder keeps the process efficient. After processing all digits, reverse the result if you built it backward.
What is the best approach for Thousand Separator?
The most reliable approach is processing the number as a string from right to left and inserting a dot after every three digits. This runs in O(n) time and O(n) space where n is the number of digits. It avoids complex index calculations and works consistently in languages like C++, Java, Python, and JavaScript.
Is Thousand Separator asked at Google/Amazon/Meta?
This problem is categorized as an easy string formatting task and appears mainly in practice sets or screening rounds. While not among the most frequently reported interview questions at Google or Meta, it reflects common string manipulation patterns that appear in real interview questions.
What data structure is used in Thousand Separator?
The solution primarily uses strings and sometimes a mutable buffer such as StringBuilder, list, or character array to build the formatted result efficiently. No advanced data structures are required.
What is the time complexity of Thousand Separator?
The optimal solution runs in O(n) time because each digit of the number is processed exactly once. Space complexity is O(n) since the algorithm builds a new formatted string containing digits and separator characters.

Ready to solve this problem?

Practice Thousand Separator with our built-in code editor and test cases.

Practice on FleetCode