Fraction to Recurring Decimal - Complete Solution Guide
Fraction to Recurring Decimal is LeetCode problem 166, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format . If the fractional part is repeating, enclose the repeating part in parentheses If multiple answers are possible, return any of them . It is guaranteed that the length of the answer string is less than 10 4 for all the given inputs. Note that if the fraction can be represented as a finite length string , you must return it. Example 1: Input: numerator = 1, denominator = 2 Output: "0
Detailed Explanation
The problem asks us to convert a fraction, represented by its numerator and denominator, into a string format. If the decimal representation has a repeating part, we need to enclose that repeating part in parentheses. For instance, 1/2 becomes "0.5", 2/1 becomes "2", and 4/333 becomes "0.(012)". We are guaranteed that the length of the resulting string is less than 10000. It's important to handle potential negative inputs for both the numerator and denominator. The problem requires handling both integer results (no decimal part) and decimal results that either terminate or recur.
Solution Approach
The provided solutions simulate long division. First, determine the sign of the result. Then calculate the integer part of the fraction. If the remainder is 0 at this point, the fraction is a whole number, and we are done. Otherwise, start calculating the decimal part. Use a hash map to store each remainder encountered and its corresponding index in the result string. If a remainder repeats, it means the decimal part from that index repeats, and we enclose it in parentheses.
Step-by-Step Algorithm
- Step 1: Handle the sign. Determine if the result should be negative by checking if the numerator and denominator have different signs.
- Step 2: Convert numerator and denominator to their absolute values (long type) to avoid potential integer overflow during calculations.
- Step 3: Calculate the integer part of the fraction (numerator // denominator) and append it to the result string.
- Step 4: Calculate the initial remainder (numerator % denominator). If the remainder is 0, the fraction is an integer, so return the result.
- Step 5: If the remainder is not 0, append a decimal point "." to the result string.
- Step 6: Initialize a hash map (or array in C) to store remainders and their corresponding indices in the result string.
- Step 7: Enter a loop that continues as long as the remainder is not 0.
- Step 8: Inside the loop, check if the current remainder is already in the hash map. If it is, it means we have found a repeating decimal. Insert a "(" at the index stored in the hash map and append a ")" at the end of the result string, then break out of the loop.
- Step 9: If the remainder is not in the hash map, store the remainder and its current index in the result string into the hash map.
- Step 10: Multiply the remainder by 10 to get the next digit in the decimal part.
- Step 11: Calculate the next digit (remainder // denominator) and append it to the result string.
- Step 12: Update the remainder (remainder % denominator) for the next iteration.
- Step 13: After the loop finishes (either by finding a repeating decimal or the remainder becoming 0), return the result string.
Key Insights
- Insight 1: The core idea is to simulate long division to obtain the decimal representation.
- Insight 2: A hash map (or array in C) is crucial to detect repeating remainders. If a remainder repeats, the decimal digits generated from that point onwards also repeat.
- Insight 3: We need to handle negative numerator or denominator. To avoid integer overflow issues, especially when numerator and denominator are close to the limits of integer range, converting them to long type before taking absolute value is important.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, Math, String.
Companies
Asked at: Airbnb, Goldman Sachs, Google, IXL, ServiceNow, TikTok.