Easy and Simple C++ Code ☠️💯✅
Code\ncpp []\nclass Solution {\npublic:\n    string fractionAddition(string s) {\n        int num = 0;\n        int den = 1;\n        int n = s.size();\n       
# Code\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string s) {\n        int num = 0;\n        int den = 1;\n        int n = s.size();\n        int i = 0;\n        while (i < n) {\n            int cn = 0;\n            int cd = 0;\n            bool neg = false;\n            char ch = s[i];\n            if (ch == \'+\' || ch == \'-\') {\n                if (ch == \'-\') {\n                    neg = true;\n                }\n                i++;\n            }\n            int st = i;\n            while (i < n && isdigit(s[i])) {\n                i++;\n            }\n            cn = stoi(s.substr(st, i - st));\n            if (neg) {\n                cn *= -1;\n            }\n            i++;\n            st = i;\n            while (i < n && isdigit(s[i])) {\n                i++;\n            }\n            cd = stoi(s.substr(st, i - st));\n            num = num * cd + cn * den;\n            den = den * cd;\n            int g = gcd(abs(num), den);\n            num /= g;\n            den /= g;\n        }\n        return to_string(num) + "/" + to_string(den);\n    }\n};\n\n```\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Initialization:**\n\nTwo variables, num (numerator) and den (denominator), are initialized to represent the result of the accumulated fraction. Initially,` num = 0` and `den = 1`, representing the fraction `0/1`.\n- n is the size of the input string s.\n- i is used as a pointer to traverse through the input string.\n\n**Iterate Over the String:**\n\nThe code enters a while loop that continues as long as i < n, meaning it processes each fraction in the string.\nInside the loop, the local variables cn (current numerator), cd (current denominator), and neg (a boolean flag to check if the fraction is negative) are initialized.\n\n**Handling Signs:**\n\nThe character ch = s[i] is checked to see if it\'s a + or -. If it\'s -, neg is set to true to handle the negative sign. i is then incremented to move to the next character.\n\n**Extracting Numerator:**\n\n`st = i` marks the starting index of the numerator. The code then moves the pointer i forward while the characters are digits, effectively parsing the numerator part of the fraction.\n`cn = stoi(s.substr(st, i - st))` converts the extracted substring to an integer representing the current numerator.\nIf neg is true, cn is multiplied by -1 to account for the negative sign.\n\n**Extracting Denominator:**\n\nAfter parsing the numerator, **i++ skips the / character**.\nSimilar to the numerator extraction, st = i marks the start of the denominator, and the loop increments i until all digit characters are processed.\n`cd = stoi(s.substr(st, i - st)) `converts the extracted substring to an integer representing the current denominator.\n\n**Updating the Result:**\n\nThe result is updated using the formula for adding fractions: num = num * cd + cn * den, and den = den * cd. This adjusts the result to account for the common denominator of the fractions.\nTo ensure the fraction remains in its simplest form, the greatest common divisor (GCD) of the numerator (num) and the denominator (den) is computed using gcd(abs(num), den).\nBoth num and den are divided by their GCD to simplify the fraction.\n\n**End of Loop:**\n\nThe loop continues until all characters of the string have been processed.\n\n**Returning the Result:**\n\nThe final result is converted to a string in the form "num/den" and returned.\n\n# Complexity\n- Time complexity: O(n)\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
['C++']