Python 1556.py
class Solution:
def thousandSeparator(self, n: int) -> str:
return f"{n:,}".replace(",", ".") # workaround: dot is a special symbol for F-strings
Difficulty
and has an acceptance rate of about 54% (100k+ submissions).
Typical patterns to solve this problem:
- String
AI Disclaimer
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.
See this dedicated page for more details.
Code Walkthrough
Overview
The provided implementation defines a single method thousandSeparator inside a class Solution. Its purpose is to convert an integer into a string representation where groups of three digits are separated by a dot (.) instead of the default comma. The method leverages Python’s formatted string literals (f-strings) to apply locale-independent grouping and then performs a simple character replacement to meet the required separator format.
Key Steps
The core logic consists of two operations performed in a single return statement.
return f"{n:,}".replace(",", ".")
The first operation, f"{n:,}", invokes the built-in formatting mini-language with the , option. This inserts commas at every thousandth position, producing a string such as "1,234,567" for the input 1234567.
The second operation, .replace(",", "."), scans the resulting string and substitutes each comma with a dot, yielding "1.234.567". The comment clarifies that the dot is a special symbol for f-strings, which is why the replacement is performed after formatting rather than directly in the format specifier.
Edge Cases
The code gracefully handles the situation where the integer is less than one thousand. In that case the formatting operation produces a string without any commas, for example "999" for the input 999. The subsequent replace call finds no commas to substitute, leaving the string unchanged, which correctly represents the number without any separators.
Negative numbers are also accommodated because the formatting mini-language retains the leading minus sign while still inserting commas for the absolute value. For instance, -1234567 becomes "-1,234,567" after formatting and "-1.234.567" after replacement, preserving the sign and grouping semantics.
Zero is another trivial edge case; formatting yields "0" and the replace step has no effect, resulting in the correct output "0".
Performance Optimizations
The implementation relies on Python’s native string formatting machinery, which is implemented in C and therefore executes faster than a manual loop that assembles the result character by character. By delegating the grouping work to the interpreter’s optimized path, the method avoids Python-level overhead associated with repeated string concatenation or list construction.
The subsequent replace call operates in a single pass over the formatted string, scanning each character once and constructing a new string only when a comma is encountered. This linear scan is efficient and does not introduce additional data structures.
Overall, the combination of built-in formatting and a single replace operation yields a concise solution that executes with minimal interpreter overhead while still producing the desired output format.
ELI5 - Simplified Explanation
Imagine you have a giant pile of colorful building blocks, and you want to organize them into neat little stacks so they are easier to count. The code acts like a helper who looks at a long number and decides where to put tiny dots to act as labels. First, the helper takes the big number and puts a comma after every three blocks, just like in a recipe or a storybook. Because some places use dots instead of commas to mark these spots, the helper then goes through the number one last time and swaps every comma for a dot. Now the long number is tucked away in little groups where everyone can see it clearly.
Code Quality
Complexity Analysis
Other Solutions
Obviously, I did that after I solved the problem myself.
In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.
And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.
(click on a column header to sort the table)
| author | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| author | solutions | |||||||||
| www | AlgoMonster | C++ | Python | Java | TS | |||||
| WalkCCC | C++ | Python | Java | |||||||
| leetcode.ca | C++ | Python | Java | Go | ||||||
| Progiez | C++ | Python | Java | |||||||
| github | black-shadows | C++ | ||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| fartem | Ruby | |||||||||
| FelixFan1122 | Java | |||||||||
| fsq | C++ | |||||||||
| haoel | C++ | |||||||||
| JoshCrozier | JS | |||||||||
| keineahnung2345 | C++ | |||||||||
| Krazune | C++ | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| trung-hn | Python | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python | |||||||||
| AlgoMonster | C++, Python, Java, TypeScript | |||||||||
| WalkCCC | C++, Python, Java | |||||||||
| leetcode.ca | C++, Go, Java, Python | |||||||||
| Progiez | C++, Java, Python | |||||||||
| black-shadows | C++, C++ | |||||||||
| charles-wangkai | Java | |||||||||
| DimitrisJim | C | |||||||||
| fartem | Ruby | |||||||||
| FelixFan1122 | Java | |||||||||
| fsq | C++ | |||||||||
| haoel | C++ | |||||||||
| JoshCrozier | JavaScript | |||||||||
| keineahnung2345 | C++ | |||||||||
| Krazune | C++ | |||||||||
| lkwq007 | Python | |||||||||
| muhammadfarhankt | Go | |||||||||
| suxrobGM | C# | |||||||||
| trung-hn | Python | |||||||||
| varunu28 | Java | |||||||||
| YuriSpiridonov | Python |
Videos
Solved On The Same Day
- problem 1317: Convert Integer to the Sum of Two No-Zero Integers
- problem 1417: Reformat The String
- problem 1790: Check if One String Swap Can Make Strings Equal
- problem 2259: Remove Digit From Number to Maximize Result
- problem 2582: Pass the Pillow
- problem 2605: Form Smallest Number From Two Digit Arrays
- problem 2729: Check if The Number is Fascinating
- problem 881: Boats to Save People
- problem 1503: Last Moment Before All Ants Fall Out of a Plank
- problem 1578: Minimum Time to Make Rope Colorful
- problem 1685: Sum of Absolute Differences in a Sorted Array
- problem 1701: Average Waiting Time
Extra
- Tech Stack - my development environment and tools
- AI - how AI helped me create this website (NOT solving the problems)
- Progress - progression over time
- Quality - code quality