Fraction Addition and Subtraction - Easy Explanation with the Gcd - Super Understandable Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nTo split the string first based on the operator and then in that string,split it based 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo split the string first based on the operator and then in that string,split it based on "/" as numerator and denominator.\n\nnumerator value is initially set to 0 and the denominator value is initially set to 1.\n\nAnd then iteratively perform operation by adding "0/1" with the incoming numbers.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Split the string based on the operators.\n2) Initialize the numerator to 0 and the denominator to 1.\n3) Iterate through the loop for each of the string.\n4) In the iteration,split the string based on "/" as current numerator and for current denominator(In our case: num and denom).\n5) Then perform addition or subtraction operation by adding with "0/1" with incoming numbers by numerator as numerator*denom + num*denominator as denominator as denominator*=denom.\n6) Find the gcd of absolute value of numerator and the denominator by using gcd() function.\n7) Then divide the numerator by gcd and denominator by gcd.\n8) Iterate the loop until all the number strings.\n9) Then return the answer as numberator+"/"+denominator.\n\n**gcd function**: \n     This function get two values : numerator and denominator as a and b.\n     This function repeats the iteration till b is not equal to 0.\n     At each iteration,change the value of a to b and then b to a%b.\n     Then a is the gcd when b becomes 0.\n\n# Complexity\nTime complexity:**O(n)**. \n\n  n -> length of the input string.\n- The time complexity for splitting the expression is **O(n)**.\n- The loop through the fraction contributes **O(mlogC)** due to GCD Calculations.\n- m is constant and small,the denominator factor is the length of the String so overall complexity is **O(n+logC)**.\n- But in the constraints,maximum value of m is given as 10,the time complexity is essentially **O(n)**.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\nSpace complexity:**O(1)**.\n\n- Since extra space is used for storing the number as string but m is given as small and constant.Hence the overall space complexity is \n**O(1)** -> constant time complexity.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    private int gcd(int a,int b)\n    {\n        while(b!=0)\n        {\n            int temp=b;\n            b=a%b;\n            a=temp;\n        }\n        return a;\n    }\n    public String fractionAddition(String expression) {\n       String[] fractions=expression.split("(?=[+-])");\n       int numerator=0,denominator=1;\n       for(String frac:fractions)\n       {\n        String[] parts=frac.split("/");\n        int num=Integer.parseInt(parts[0]);\n        int denom=Integer.parseInt(parts[1]);\n        numerator=numerator*denom+num*denominator;\n        denominator*=denom;\n\n        int gcd=gcd(Math.abs(numerator),denominator);\n\n        numerator/=gcd;\n        denominator/=gcd;\n       }\n       return numerator+"/"+denominator;\n    }\n}\n```
['Math', 'String', 'Simulation', 'Java']