Easy solution in C++
Intuition\n Describe your first thoughts on how to solve this problem. \nString is valid, So when \'/\' is encountered we will have to get the numerator (left s
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nString is valid, So when \'/\' is encountered we will have to get the numerator (left side) and denominator(right side) to calculate the fraction, if sign is encounter set the sign as positive or negative to push numerator of correct sign.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Parsing the Expression:\n- The input string expression contains fractions separated by either + or - signs.\n- Each fraction has a numerator and a denominator, separated by a / symbol.\n- The goal is to extract these fractions and their corresponding signs, then compute the sum or difference of these fractions.\n\n2. Handling Multiple Digits:\n- Since the numerators and denominators can have multiple digits, carefully parse the entire number before and after the /.\n- Use the existing logic to handle positive and negative signs.\n\n3. Common Denominator Calculation:\n- To add or subtract fractions, they must be converted to have a common denominator.\n- The least common multiple (LCM) of the denominators is used to find this common denominator.\n\n4. Summing the Fractions:\n- Convert each fraction to the common denominator and sum their numerators.\n- Handle the sign of the fraction based on the parsed sign (+ or -).\n\n5. Simplifying the Result:\n- After summing the fractions, the result should be simplified by dividing both the numerator and the denominator by their greatest common divisor (GCD).\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# Code\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string expression) {\n        vector<pair<int, int>> fraction;\n        int n = expression.size();\n        bool isPos = true;\n        int val = 1;\n        for (int i = 0; i < n; i++) {\n            if (expression[i] == \'+\') {\n                isPos = true;\n            }\n            else if (expression[i] == \'-\') {\n                isPos = false;\n            }\n            if (expression[i] == \'/\') {\n                int num = expression[i - 1] - \'0\', den = expression[i + 1] - \'0\';\n                if (i > 1 && expression[i - 2] >= \'0\' && expression[i - 2] <= \'9\') {\n                    num = num + (expression[i - 2] - \'0\') * 10;\n                }\n                if (i < n - 1 && expression[i + 2] >= \'0\' && expression[i + 2] <= \'9\') {\n                    den = den * 10 + (expression[i + 2] - \'0\');\n                }\n                val = lcm(den, val);\n                if (!isPos) {\n                    fraction.push_back({-num, den});\n                }\n                else \n                    fraction.push_back({num, den});\n            }\n        }\n        n = fraction.size();\n        int num = 0, den = val;\n        for (int i = 0 ; i < n; i++) {\n            int temp = fraction[i].first * val / fraction[i].second;\n            num += temp;\n        }\n        if (num == 0) return "0/1";\n        return to_string(num / gcd(num, den)) + "/" + to_string(den / gcd(num, den));\n    }\n};\n```
['Math', 'C++']