Domain 1EasyCORE

Thousand Separator

The problem requires converting an integer into its string representation with dots (".") inserted as thousands separators. The input is a non-negative integer `n` within the range of 0 to (2^{31} - 1). The output should be a string where every three digits from the right are separated by a dot.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

1556. Thousand Separator

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 57.2%


Problem Breakdown

The problem requires converting an integer into its string representation with dots (”.”) inserted as thousands separators. The input is a non-negative integer n within the range of 0 to (2^{31} - 1). The output should be a string where every three digits from the right are separated by a dot.

Inputs:

  • An integer n.

Outputs:

  • A string representation of n with thousands separators.

Implicit Constraints or Edge Cases:

  • If n is 0, the output should simply be “0”.
  • The number can have up to 10 digits (since (2^{31} - 1) has 10 digits).

Intuition

A brute-force approach would involve converting the integer to a string and then manually inserting dots at every third position from the right. However, this method is inefficient as it requires iterating over the string multiple times.

The key insight is to build the result string by processing the number from least significant digit to most significant digit (right to left). This allows us to insert dots naturally without needing to reverse the string later.

Approach

  1. Convert the integer to a string: This allows easy access to each digit.
  2. Initialize an empty list for the result: We’ll build the final string by appending characters to this list.
  3. Iterate over the digits from right to left:
    • Append the current digit to the result list.
    • If the number of processed digits is a multiple of three and it’s not the first digit, append a dot.
  4. Reverse the result list: Since we built the string from right to left, we need to reverse it to get the correct order.
  5. Join the list into a single string and return it.

Complexity Analysis

  • Time Complexity: (O(d)), where (d) is the number of digits in the integer. This is because we process each digit exactly once.
  • Space Complexity: (O(d)) for storing the result list, which will have at most (d + \lfloor d/3 \rfloor) characters (including dots).

Solution

def thousandSeparator(n: int) -> str:
    if n == 0:
        return "0"
    
    num_str = str(n)
    result = []
    count = 0
    
    for i in range(len(num_str) - 1, -1, -1):
        result.append(num_str[i])
        count += 1
        if count % 3 == 0 and i != 0:
            result.append('.')
    
    return ''.join(result[::-1])

Edge Cases Handled

  • Zero: The function returns “0” as expected.
  • Single-digit numbers: Numbers like 987 are returned without any dots.
  • Numbers with exactly three digits: These are also returned without any dots.
  • Numbers with a multiple of three digits: Dots are correctly placed after every third digit from the right.
  • Numbers with non-multiple of three digits: The last group may have fewer than three digits, but no dot is added at the end.