C++ Beats 100%!!! Pointwise explanation!
\n\n1. gcd Function:\nCalculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.\n2. lcm Function:\nComputes the least common mu
![image.png](https://assets.leetcode.com/users/images/be4430a7-7467-4422-bb3a-8536b6f857f8_1724385933.063961.png)\n\n1. **gcd Function:**\nCalculates the greatest common divisor (GCD) of two numbers using the Euclidean algorithm.\n2. **lcm Function:**\nComputes the least common multiple (LCM) of two numbers using the formula: LCM(a, b) = (a / GCD(a, b)) * b.\n\n3. **reduce Function:**\nSimplifies a fraction by dividing both the numerator and the denominator by their GCD.\n\n4. **parseNumber Function:**\nExtracts a potentially multi-digit number from the string and handles its sign (positive or negative).\n\n5. **fractionAddition Function:**\nInitialization: Starts with an initial fraction of 0/1.\nParsing: Iterates through the input string, parsing each fraction one by one.\nLCM Calculation: Finds the LCM of the current and next denominators to bring them to a common base.\nNumerator Update: Updates the cumulative numerator based on the current operation (+ or -).\nDenominator Update: Sets the denominator to the LCM.\nSimplification: After processing all fractions, simplifies the final result.\nReturn: Outputs the result as a string in the form "numerator/denominator".\n\n\n# Please Upvote!\n# Code\n```cpp []\nclass Solution {\n    int gcd(int a, int b) {\n        if (b == 0) return a;\n        return gcd(b, a % b);\n    }\n    \n    int lcm(int a, int b) {\n        return (a / gcd(a, b)) * b;\n    }\n    \n    void reduce(int &a, int &b) {\n        int gcd_ab = gcd(abs(a), b);\n        a /= gcd_ab;\n        b /= gcd_ab;\n    }\n    int parseNumber(const string &expression, int &i) {\n        int sign = 1;\n        if (expression[i] == \'-\') {\n            sign = -1;\n            i++;\n        } \n        else if (expression[i] == \'+\')\n            i++;\n        int num = 0;\n        while (i < expression.length() && isdigit(expression[i])) {\n            num = num * 10 + (expression[i] - \'0\');\n            i++;\n        }\n        return sign * num;\n    }\n    \npublic:\n    string fractionAddition(string expression) {\n        int numerator = 0, denominator = 1;\n        for (int i = 0; i < expression.length();) {\n            int num = parseNumber(expression, i);\n            i++; // Skip the \'/\'\n            int den = parseNumber(expression, i);\n            int LCM = lcm(denominator, den);\n            numerator = numerator * (LCM / denominator) + num * (LCM / den);\n            denominator = LCM;\n        }\n        if (numerator == 0) return "0/1";\n        reduce(numerator, denominator);\n        return to_string(numerator) + "/" + to_string(denominator);\n    }\n};\n```
['C++']