Easy and explained step-by-step approach 🔥
Intuition\nThe problem requires us to add and subtract fractions represented as a string expression. Since fractions can have different denominators, we need to
# Intuition\nThe problem requires us to add and subtract fractions represented as a string expression. Since fractions can have different denominators, we need to bring them to a common denominator before performing addition or subtraction. The output should be a simplified fraction.\n# Approach\n- Parse the Input Expression:\n  - Traverse through the input string to extract all the fractions and store them in a list (map). Each fraction is represented as a pair of integers (numerator and denominator).\n\n- Calculate the Least Common Multiple (LCM):\n  - To perform addition or subtraction, compute the LCM of all denominators to bring all fractions to a common denominator.\n\n- Adjust Numerators and Compute the Resulting Numerator:\n  - Adjust the numerators according to the common denominator and sum them up to get the total numerator.\n\n- Simplify the Resulting Fraction:\n  - Compute the greatest common divisor (GCD) of the numerator and the denominator to simplify the resulting fraction to its lowest terms.\n\n- Return the Result:\n  - Return the simplified fraction in the form of a string.\n\n# Code Explanation\n- fillMap Method: Parses the input string to extract all fractions and store them as integer pairs (numerator, denominator) in the list map.\n\n- LCM and gcd Methods: Helper functions to calculate the least common multiple and greatest common divisor, respectively. These are used to find the common denominator and to simplify the final result.\n\n- fractionAddition Method: Main method that handles the entire process: filling the map with fractions, calculating the LCM, adjusting the numerators, summing them up, and simplifying the result.\n\n# Complexity\n- Time complexity:\n  - O(n+k), where n is the size of string and k is the number of fractions in the string.\n- Space complexity:\n    O(k),  where k is the size of map.\n# Code\n```java []\nclass Solution {\n\n    private int gcd(int a, int b){\n        while(b != 0){\n            int temp = b;\n            b = a % b;\n            a = temp;\n        }\n        return a;\n    }\n    private int LCM(int a, int b){\n        return Math.abs(a * (b / gcd(a,b)));\n    }\n\n    public String fractionAddition(String expression) {\n        List<int[]> map = new ArrayList<>();\n        fillMap(expression.toCharArray(), map);\n\n        int numerator=0, denominator=1;\n\n        for (int[] fraction : map) {\n            denominator = LCM(denominator, fraction[1]);\n        }\n\n        for (int[] fraction : map) {\n            numerator += fraction[0] * (denominator / fraction[1]);\n        }\n\n        int gcd = gcd(Math.abs(numerator), denominator);\n        numerator /= gcd;\n        denominator /= gcd;\n\n        return numerator+"/"+denominator;\n\n    }\n\n    private void fillMap(char[] array, List<int[]> map){\n        int N, D, i=0;\n        int positive=1, length = array.length;\n\n        while(i < length){\n            N=0; D=0;\n            if(array[i] == \'-\') {\n                positive=-1;\n                i++;\n            } else if(array[i] == \'+\'){\n                positive=1;\n                i++;\n            }\n\n            while(i < length && array[i] != \'/\'){\n                N = (N * 10) + (int)array[i] - \'0\';\n                i++;\n            }\n            i++;\n            while(i < length && array[i] != \'+\' && array[i] != \'-\'){\n                D = (D * 10) + (int)array[i] - \'0\';\n                i++;\n            }\n            map.add(new int[] {positive*N, D});\n        }\n    }\n}\n```
['Math', 'Java']