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)