LeetCode 592: Fraction Addition and Subtraction Solution

Master LeetCode problem 592 (Fraction Addition and Subtraction), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

592. Fraction Addition and Subtraction

Problem Explanation

Explanation

To solve this problem, we can split the input string by either '+' or '-' to get individual fractions. Then we can add or subtract these fractions accordingly. To add or subtract fractions, we need to find a common denominator. After performing the operation, we need to simplify the resulting fraction by finding the greatest common divisor (GCD) of the numerator and denominator.

Algorithm:

  1. Split the input string by either '+' or '-' to get individual fractions.
  2. For each fraction, extract the numerator and denominator.
  3. Initialize the result numerator and denominator as 0 and 1 respectively.
  4. For each fraction:
    • Calculate the new numerator and denominator after adding or subtracting based on the sign.
    • Find the GCD of the new numerator and denominator.
    • Divide the new numerator and denominator by their GCD to simplify the fraction.
  5. Return the simplified fraction as the final result.

Time Complexity: O(n), where n is the length of the input string.

Space Complexity: O(1)

Solution Code

class Solution {
    public String fractionAddition(String expression) {
        String[] fractions = expression.split("(?=[+-])");
        int resNumerator = 0, resDenominator = 1;
        
        for (String fraction : fractions) {
            String[] parts = fraction.split("/");
            int numerator = Integer.parseInt(parts[0]);
            int denominator = Integer.parseInt(parts[1]);
            
            resNumerator = resNumerator * denominator + (fraction.charAt(0) == '-' ? -1 : 1) * numerator * resDenominator;
            resDenominator = resDenominator * denominator;
            
            int gcd = gcd(Math.abs(resNumerator), resDenominator);
            resNumerator /= gcd;
            resDenominator /= gcd;
        }
        
        return resNumerator + "/" + resDenominator;
    }
    
    private int gcd(int a, int b) {
        while (b != 0) {
            int temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 592 (Fraction Addition and Subtraction)?

This page provides optimized solutions for LeetCode problem 592 (Fraction Addition and Subtraction) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 592 (Fraction Addition and Subtraction)?

The time complexity for LeetCode 592 (Fraction Addition and Subtraction) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 592 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 592 in Java, C++, or Python.

Back to LeetCode Solutions