Understanding the Problem
Thousand Separator is an easy problem at leetcode, I hope you have read the problem carefully and have tried solving it. This problem requires to put a thousand separator in its right position of the number. The question is how you can iterate on such a number to write a dot (.) in every possible position.
Why regular expression?
You’re given an integer and the output should be a string with the thousand separators. Dealing with strings can be cumbersome and a problem like this can take multiple lines of code while you can do it literally in one-liner code if you really compacted the variables. While regular expression (shortened regex) can be seen complicated, this is due to the unclear readability of it, I will try to explain solving this problem in a more readable approach and a more expressing way to be able to understand and solve such problem in just one line of code and if you need to get deeper, I’ve put some references at the bottom.
Regular expression is dealing with patterns so that you can manipulate strings. In this problem, it seems we want to find a pattern that exists across the string with multiple positions and then put dots in every possible position.
How regex works in this problem?
To know how it works, we need to know two things:
- pattern - the pattern we want to match
- repl - what should be replaced with
Example 1
Say we have this simple example 1234. The separator here should exist between 1 and 2 like this 1.234
Using regular expression can make us search for a pattern and when we match this pattern correctly we can use it to substitute/replace the number(s) we captured. Grouping is a basic concept of regex, you can use parenthesis to group specific characters.
The pattern here can be one digit followed by 3 digits . This can be converted to a regular expression pattern like this (\d)(?=(\d{3}))
(\d) one digit…
(?= followed by…
(\d{3}) 3 digits
)
The pattern here should match 1 in 1 234
So now we have the pattern, what else should we do with that?
It seems we have captured two groups here, one digit and 3 digits .. we’re just interested in the first group so we should replace that digit ( 1 here) with itself followed by a dot so that we can see 1. 234. That’s why repl here equals \1. or \\$1. depending on the language you use
Let’s say we want the separator here to be a comma not a dot, repl here should equal \1, and \\$1,
Example 2
Let’s see a more complicated example: 123456789 so the expected output should be 123.456.789
The pattern here is different from the previous example because if it’s the same, the grouping will be to these bold numbers 123456 789 because in each number is followed by 3 digits until you reach 789 (the last 3 digits). So how can we approach this solution to only group the numbers that we should replace each and add dots?
Here we should group the 3 digits in a right way; we want to capture one digit which is followed by one or more groups of 3 digits which are not followed by any more digits . Let’s convert that into a regular expression pattern: (\d)(?=(\d{3})+(?!\d))
(\d) capture one digit…
(?= which is followed by…
(\d{3})+ one or more groups of 3 digits…
(?!\d) which are not followed by any more digits
)
With this pattern, numbers highlighted will be matched 12 3 45 6 789. There you should replace both by each one plus a dot like this: 12 3. 45 6. 789
That’s how I thought about this problem and below is my implementations in Python and Javascript:
Python
Javascript
Motivated by
- Regular expression operators documentation in Python
- All you need to know about Regular Expressions
- How to use regex to replace a string with itself?
- How to print number with commas as thousands separators?
- What does an ‘r’ represent before a string in python?
- Online tool for playing with regular expressions
More problem-solving?
If you want to see more problem-solving blog posts, check out:
https://www.ezzeddinabdullah.com/categories/problem-solving
Want to support me?
Please clap on medium if you like this
article
, thank you! :)