100beats | ✅✅| Easy | CPP | String Simulation |
Intuition\nSolve simillar to that we used to solve in our maths class.\n\n# Approach\n- Find the denominators of all the fraction and take lcm of all that to fi
# Intuition\nSolve simillar to that we used to solve in our maths class.\n\n# Approach\n- Find the denominators of all the fraction and take lcm of all that to find the final denominator\n- Update the numerator according to the lcm.\n- Simplify the last numerator and the denominator.\n\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string exp) {\n        \n        multiset<pair<int, int>> hash;\n        vector<int> deno;   \n\n        if(isdigit(exp[0])) exp = \'+\' + exp;\n\n        int n = exp.size();\n\n        for(int i = 0; i + 3 < n; i++) {      \n            if(exp[i] == \'-\') {\n                int x = -1 * ((exp[i + 1]) - \'0\');\n                int f = 0;\n                if(i + 2 < n and exp[i + 2] == \'0\') {\n                    x *= 10;\n                    f = 1;\n                }\n                int y = exp[i + 3 + f] - \'0\';\n                if(i + 4 < n and exp[i + 4] == \'0\') {\n                    y *= 10;\n                }\n                hash.insert({x, y});\n                deno.push_back(y);\n            } \n\n            if(exp[i] == \'+\') {\n                int x = 1 * ((exp[i + 1]) - \'0\');\n                int f = 0;\n                if(i + 2 < n and exp[i + 2] == \'0\') {\n                    x *= 10;\n                    f = 1;\n                }\n                int y = exp[i + 3 + f] - \'0\';\n                if(i + 4 < n and exp[i + 4] == \'0\') {\n                    y *= 10;\n                } \n                hash.insert({x, y});\n                deno.push_back(y);\n            }\n        }\n\n        int lc = 1;\n\n        for(auto it : deno) lc = (lc * it) / __gcd(lc, it);\n\n        string ans = "";\n\n        vector<int> numer;\n\n        for(auto it : hash) {\n            if(it.second == lc) {\n                numer.push_back(it.first);\n            } else {\n                numer.push_back(it.first * (lc / it.second));\n            }\n        }\n\n        int a = 0;\n\n        for(auto it : numer) a += it;\n\n        if(a == 0) {\n            return "0/1";\n        }\n\n        if(a < 0) {\n            ans += \'-\';\n        }\n        a = abs(a);\n        for(int i = 2; i <= max(a, lc); i++) {\n            while(lc % i == 0 and a % i == 0) {\n                lc /= i;\n                a /= i;\n            } \n\n        }\n        if(a >= 10) {\n            ans += to_string(a);\n        } else {\n            ans += (a + \'0\');\n        }\n        ans += \'/\';\n        if(lc >= 10) {\n            ans += to_string(lc);\n        } else {\n            ans += (lc + \'0\');\n        }\n\n        return ans;\n    }\n};\n```
['String', 'Simulation', 'C++']