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.

&nbsp;
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"
Output: "-1/6"


&nbsp;
Constraints:


	The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
	Each fraction (input and output) has the format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
	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.
	The number of given fractions will be in the range [1, 10].
	The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.


"""
approach:
first replace - with +- in the string so that implementation gets
a little easy
"""
class Solution:
    def fractionAddition(self, expression: str) -> str:
        expression = expression.replace('-', '+-')
        parts = [item for item in expression.split('+') if item != '']
        numes, denoms, denom_set, lcm = [], [], set(), 1
        def get_lcm(a, b):
            if a == 1:
                return b
            if b == 1:
                return a
            if a < b:
                if b % a == 0:
                    return a * get_lcm(1, b/a)
                else:
                    return a * get_lcm(1, b)
            else:
                if a % b == 0:
                    return b * get_lcm(a/b, 1)
                else:
                    return b * get_lcm(a, 1)

        for part in parts:
            num, den = part.split('/')
            numes.append(int(num))
            denoms.append(int(den))
            lcm = get_lcm(lcm, int(den))

        result = 0
        for num, den in zip(numes, denoms):
             result +=num * int(lcm/den)

        def get_gcd(a, b):
            if a == 0:
                return b
            if b == 0:
                return a
            if a == b:
                return a
            elif a < b:
                return get_gcd(a, b-a)
            else:
                return get_gcd(a-b, b)

        gcd = get_gcd(abs(result), lcm)
        return str(int(result/gcd)) + '/' + str(int(lcm/gcd))
class Solution {
    private int gcd(int x, int y){
        return x!=0?gcd(y%x, x):Math.abs(y);
    }

    public String fractionAddition(String exp) {
        Scanner sc = new Scanner(exp).useDelimiter("/|(?=[-+])");

        int A=0, B=1;
        while(sc.hasNext()){
            int a = sc.nextInt(), b=sc.nextInt();
            A = A * b + B * a;
            B *= b;

            int gcdX = gcd(A, B);
            A/=gcdX;
            B/=gcdX;
        }
        return A+"/"+B;
    }
}
class Solution {
public:
    string fractionAddition(string expression) {
        
        int res_num = 0; //keep track of numerator
        int res_denom = 1; //keep track of denominator
        char sign = '+'; //keep track of sign
		
        for(int i = 0; i < expression.size(); i++){ //parse the expression string
            int next_num = 0;
            int next_denom = 0;
            if(expression[i] == '+' || expression[i] == '-') //updating
                sign = expression[i];
            else{
                int j = i+1;
                while(j < expression.size() && expression[j] != '/') j++; build next numerator
                next_num = stoi(expression.substr(i, j-i));
                int k = j+1;
                while(k < expression.size() && expression[k] >= '0' && expression[k] <= '9') k++; //build next denominator
                next_denom = stoi(expression.substr(j+1, k-(j+1)));
                if(res_denom != next_denom){ //update result numerator and denominator
                    res_num *= next_denom;
                    next_num *= res_denom;
                    res_denom *= next_denom;
                    }
                if(sign == '+') res_num += next_num;
                else res_num -= next_num;
                i = k-1;
            }
        }
        return lowestFraction(res_num, res_denom); //put the fraction into lowest terms and return as string
    }
    
private:
    int findGCD(int a, int b) { //find Greatest Common Denominator
        if(b == 0) return a;
        return findGCD(b, a % b);
   }
    
   string lowestFraction(int num, int denom){ //use GCD to put fraction into lowest terms and return as string
      int gcd;
      gcd = findGCD(num, denom);
      num /= gcd;
      denom /= gcd;
       if(denom < 0){
           denom *= -1;
           num *= -1;
       }
      return to_string(num) + '/' + to_string(denom);
}
    
};
var fractionAddition = function(expression) {
	const fractions = expression.split(/[+-]/).filter(Boolean);
	const operator = expression.split(/[0-9/]/).filter(Boolean);
	expression[0] !== '-' && operator.unshift('+');

	const gcd = (a, b) => b === 0 ? a : gcd(b, a % b);
	const lcm = fractions.reduce((result, fraction) => {
		const denominator = fraction.split('/')[1];
		return result * denominator / gcd(result, denominator);
	}, 1);

	const molecularSum = fractions.reduce((total, fraction, index) => {
		const [molecular, denominator] = fraction.split('/');
		const multiple = lcm / denominator * (operator[index] === '+' ? 1 : -1);
		return total + molecular * multiple;
	}, 0);

	const resultGcd = gcd(Math.abs(molecularSum), lcm);
	return `${molecularSum / resultGcd}/${lcm / resultGcd}`;
};
Fraction Addition and Subtraction