Easy C++ Solution with Video Explanation ✅
Video Explanation\nhttps://youtu.be/yaiGPCQh5cY\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires performing arit
# Video Explanation\nhttps://youtu.be/yaiGPCQh5cY\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires performing arithmetic on fractions in a string format. To add or subtract fractions, they must be brought to a common denominator. Once this is achieved, the fractions can be added or subtracted by simply manipulating the numerators.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Initialization:**\n\n- Start with a numerator of 0 and a denominator of 1. This represents the initial fraction as 0/1.\n- Use an index i to traverse the string expression.\n2. **Processing the String:**\n\n- Traverse the string character by character.\n- Handle the signs + and -. If a - is encountered, set sign to -1; otherwise, it remains 1.\n- Extract the numerator and denominator for each fraction in the string.\n- Use the formula to add or subtract fractions:\n\n- After each addition, simplify the resulting fraction by dividing both the numerator and denominator by their greatest common divisor (GCD).\n3.** Return the Result:**\n\n- Convert the final numerator and denominator into a string formatted as "numerator/denominator".\n# Complexity\n- Time complexity:$$O(n log K)$$\nwhere \uD835\uDC3E is the maximum value of the numerator or denominator encountered.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string expression) {\n        int numerator=0,denominator=1;\n        int i=0;\n        int n=expression.size();\n\n        while(i<n){\n            int sign=1;\n            if(expression[i]==\'+\' || expression[i]==\'-\'){\n                if(expression[i]==\'-\') sign=-1;\n                i++;\n            }\n\n            int num=0;\n            while(i<n && isdigit(expression[i])){\n                num=num*10+(expression[i]-\'0\');\n                i++;\n            }\n            num*=sign;\n            i++;\n            int den=0;\n            while(i<n && isdigit(expression[i])){\n                den=den*10+(expression[i]-\'0\');\n                i++;\n            }\n            numerator=numerator*den+num*denominator;\n            denominator*=den;\n\n            int gcdVal=gcd(abs(numerator),denominator);\n            numerator/=gcdVal;\n            denominator/=gcdVal;\n\n        }\n        return to_string(numerator)+\'/\'+to_string(denominator);\n\n    }\n};\n```
['C++']