C++ | Beats 100% | 0ms | T.C. & S.C. -> O(n)
Intuition\n Describe your first thoughts on how to solve this problem. \nThe task is to evaluate a string representing a series of fraction additions and subtra
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe task is to evaluate a string representing a series of fraction additions and subtractions and return the result as a simplified fraction.\n\n---\n---\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n**1. Parsing the Expression:**\n\n- The input expression will be parsed to extract each fraction and its sign (positive or negative).\nFractions are of the form \xB1numerator/denominator and are separated by + or -.\n\n---\n\n\n**2. Initialization:**\n\n- Start by initializing the result with the first fraction. Determine its sign (positive or negative) and extract the numerator and denominator.\n\n---\n\n\n**3. Processing Fractions:**\n\n- For each subsequent fraction, determine its sign and extract the numerator and denominator.\n- Convert all fractions to have a common denominator using the least common multiple (LCM) of the denominators.\n- Update the result by adding or subtracting the new fraction\'s numerator (adjusted to the common denominator).\n\n---\n\n\n**4. Simplification:**\n\n- After processing all fractions, simplify the final result by dividing the numerator and denominator by their greatest common divisor (GCD).\n\n```\nint gcd(int a, int b) {\n    return b == 0 ? a : gcd(b, a % b);\n}\n```\n\n---\n\n\n**5. Formatting the Result:**\n\n- If the numerator is zero, the result should be 0/1 to represent zero correctly.\nEnsure the result is in the format of numerator/denominator where the denominator is always positive.\n\n---\n\n\n---\n\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n---\n\n\n---\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int gcd(int a, int b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n\n    int getNum(int &i, const string &expression) {\n        string temp = "";\n        while (expression[i] != \'/\') {\n            temp += expression[i];\n            i++;\n        }\n        return stoi(temp);\n    }\n\n    int getDen(int &i, const string &expression) {\n        string temp = "";\n        while (i < expression.size() && expression[i] != \'+\' && expression[i] != \'-\') {\n            temp += expression[i];\n            i++;\n        }\n        return stoi(temp);\n    }\n\n    string fractionAddition(string expression) {\n        int s1 = 1, i = 0;\n\n        if (expression[i] == \'-\') {\n            s1 = -1;\n            i++;\n        }\n\n        int num1 = getNum(i, expression);\n        i++;\n        int den1 = getDen(i, expression);\n\n        while (i < expression.size()) {\n            int s2 = expression[i] == \'-\' ? -1 : 1;\n            i++;\n\n            int num2 = getNum(i, expression);\n            i++;\n            int den2 = getDen(i, expression);\n\n            int lcm = (den2 * den1) / gcd(den1, den2);\n            num1 = s1 * num1 * (lcm / den1) + s2 * num2 * (lcm / den2);\n            den1 = lcm;\n\n            s1 = (num1 < 0) ? -1 : 1;\n            if (num1 < 0) num1 *= -1;\n        }\n\n        int gc = gcd(num1, den1);\n        if (gc > 1) {\n            num1 /= gc;\n            den1 /= gc;\n        }\n\n        return (s1 == -1 ? "-" : "") + to_string(num1) + "/" + to_string(den1);\n    }\n};\n```\n![Please Upvote (3).jpg](https://assets.leetcode.com/users/images/3fc0ac4f-5a5f-410f-9895-7629196511fb_1724375360.0657377.jpeg)\n\n\n\n\n
['Math', 'String', 'Simulation', 'C++']