Fraction Addition and Subtraction - Complete Solution Guide
Fraction Addition and Subtraction is LeetCode problem 592, 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 a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format. The final result should be an irreducible fraction . If your final result is an integer, change it to the format of a fraction that has a denominator 1 . So in this case, 2 should be converted to 2/1 . Example 1: Input: expression = "-1/2+1/2" Output: "0/1" Example 2: Input: expression = "-1/2+1/2+1/3" Output: "1/3" Example 3: Input: expression = "1/3-1/2" Ou
Detailed Explanation
The problem asks us to take a string representing an expression of fraction additions and subtractions (e.g., "-1/2+1/2+1/3") and return the result of the expression as a single irreducible fraction string (e.g., "1/3"). The input fractions are guaranteed to be irreducible and have numerators and denominators between 1 and 10. The final output should also be in the form of an irreducible fraction. If the result is an integer, it should be formatted as 'X/1'.
Solution Approach
The provided solutions iterate through the input string, extracting each fraction, and perform fraction addition or subtraction sequentially. For each fraction: the numerator and denominator are parsed from the string. Then, the current fraction is added to the accumulated sum using the formula a/b + c/d = (ad + bc) / bd. After each addition, the resulting fraction is simplified by dividing both the numerator and denominator by their greatest common divisor (GCD). Finally, the result is formatted as a string "numerator/denominator".
Step-by-Step Algorithm
- Step 1: Parse the input string to identify and extract all fractions as strings.
- Step 2: Initialize the `numerator_sum` to 0 and `denominator_sum` to 1 (representing 0/1 as the initial sum).
- Step 3: Iterate through the extracted fractions.
- Step 4: For each fraction, split the string into its `numerator` and `denominator` components.
- Step 5: Convert the `numerator` and `denominator` strings to integers.
- Step 6: Calculate the new `numerator_sum` and `denominator_sum` using the formula: `numerator_sum = numerator_sum * denominator + denominator_sum * numerator` and `denominator_sum = denominator_sum * denominator`.
- Step 7: Calculate the greatest common divisor (GCD) of the absolute values of `numerator_sum` and `denominator_sum`.
- Step 8: Divide both `numerator_sum` and `denominator_sum` by the GCD to simplify the fraction.
- Step 9: After iterating through all fractions, format the final `numerator_sum` and `denominator_sum` into a string of the form "numerator/denominator" and return it.
Key Insights
- Insight 1: The core idea is to iterate through the fractions, adding or subtracting each one from the current sum. This requires parsing each fraction into its numerator and denominator.
- Insight 2: To add or subtract fractions, we need a common denominator. The formula a/b + c/d = (a*d + b*c) / (b*d) is crucial for adding two fractions.
- Insight 3: Simplifying the fraction after each addition or subtraction by finding the greatest common divisor (GCD) prevents integer overflow and keeps numbers manageable. The final result must be irreducible.
- Insight 4: Proper parsing of the input string to extract numerators and denominators correctly, handling the signs (+ or -), is critical.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Math, String, Simulation.
Companies
Asked at: Goldman Sachs, IXL, Zopsmart.