DEV Community

Nitin Ahirwal
Nitin Ahirwal

Posted on

LeetCode Challenge Day 9 β€” 166. Fraction to Recurring Decimal

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 πŸš€

LeetCode #100DaysOfCode #JavaScript #ProblemSolving

Top comments (0)