Hey everyone ๐
Day 9 of my LeetCode streak! Today wasnโt the smoothest โ I was working from a cafรฉ instead of my desk โ, so no video walkthrough this time. But the streak must go on ๐ช.
๐ Problem Summary
Weโre dividing two integers and returning the result as a string.
If the decimal terminates โ just return it.
If the decimal repeats โ wrap the repeating digits in parentheses ๐.
Examples:
1/2 โ "0.5"
2/1 โ "2"
4/333 โ "0.(012)"
๐ก Key Insight
When doing long division, a repeating remainder means the digits will start repeating too.
So, track remainders in a map and insert parentheses once a cycle is detected.
Iโve covered the full intuition, step-by-step approach, edge cases, and JavaScript code on my blog ๐
๐ https://nitinahirwal.in/posts/166-FractiontoRecurringDecimal
Day 9 done โ โ excited for Day 10 tomorrow ๐
Top comments (0)