Formatted question description: https://leetcode.ca/all/592.html ﻿# 592. Fraction Addition and Subtraction

Medium

## Description

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1. So in this case, 2 should be converted to 2/1.

Example 1:

Input:“-1/2+1/2”

Output: “0/1”

Example 2:

Input:“-1/2+1/2+1/3”

Output: “1/3”

Example 3:

Input:“1/3-1/2”

Output: “-1/6”

Example 4:

Input:“5/3+1/3”

Output: “2/1”

Note:

1. The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
3. The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
4. The number of given fractions will be in the range [1,10].
5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

## Solution

This solution creates a class called Rational, which contains two data fields, numerator and denominator, and supports addition and subtraction.

Split the terms of fractions. Replace all occurrences of “-“ with “+-“, but remove the “+” at the beginning of the expression. Then split the expression using “+” to get an array that contains the separated terms.

Initially, the sum of all the fractions is 0. For each term in the array, create an object of Rational type, which is a rational number, and add the rational number to the sum, using the methods defined in class Rational.

After the sum of all fractions is obtained, convert the sum into a string of fraction, and return the string.

class Solution {
expression = expression.replaceAll("-", "+-");
if (expression.charAt(0) == '+')
expression = expression.substring(1);
String[] array = expression.split("\\+");
Rational sum = new Rational(0);
int length = array.length;
for (int i = 0; i < length; i++) {
String rationalStr = array[i];
int divisionIndex = rationalStr.indexOf('/');
if (divisionIndex < 0) {
Rational rational = new Rational(Long.parseLong(rationalStr));
} else {
long numerator = Long.parseLong(rationalStr.substring(0, divisionIndex));
long denominator = Long.parseLong(rationalStr.substring(divisionIndex + 1));
Rational rational = new Rational(numerator, denominator);
}
}
String sumStr = sum.numerator + "/" + sum.denominator;
return sumStr;
}
}

class Rational {
long numerator = 0;
long denominator = 1;

public Rational() {

}

public Rational(long numerator) {
this(numerator, 1);
}

public Rational(long numerator, long denominator) {
long gcd = gcd(numerator, denominator);
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
this.numerator = numerator / gcd;
this.denominator = denominator / gcd;
}

long newNumerator = this.numerator * rational2.denominator + rational2.numerator * this.denominator;
long newDenominator = this.denominator * rational2.denominator;
return new Rational(newNumerator, newDenominator);
}

public Rational subtract(Rational rational2) {
long newNumerator = this.numerator * rational2.denominator - rational2.numerator * this.denominator;
long newDenominator = this.denominator * rational2.denominator;
return new Rational(newNumerator, newDenominator);
}

public long gcd(long a, long b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0 && b == 0)
return 1;
while (a != 0 && b != 0) {
if (a > b) {
long temp = a;
a = b;
b = temp;
}
b %= a;
}
return a == 0 ? b : a;
}
}