String Fractions Simplified: Using LCM and GCD | Java & C++ | [Video Solution]
Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/jUq3a7Nkbo0\n\n# Code\njava []\nclass Solution {\n    public String
# Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/jUq3a7Nkbo0\n\n# Code\n```java []\nclass Solution {\n    public String fractionAddition(String expression) {\n        Map<Integer, List<int[]>> denoToSignNumMp = new HashMap<>();\n        int size = expression.length();\n        char expChrs[] = expression.toCharArray();\n        int indx = 0;\n        int sign = 1;\n\n        while (indx < size) {\n            char currChar = expChrs[indx];\n            if (currChar == \'-\') {\n                sign = -1;\n                indx++;\n            } else if (currChar == \'+\') {\n                sign = 1;\n                indx++;\n            }\n\n            int num = 0, deno = 0;\n            boolean slashFound = false;\n\n            while (indx < size && expChrs[indx] != \'+\' && expChrs[indx] != \'-\') {\n                if (expChrs[indx] == \'/\') {\n                    slashFound = true;\n                    indx++;\n                    continue;\n                }\n\n                if (!slashFound) {\n                    num = num * 10 + (expChrs[indx] - \'0\');\n                } else {\n                    deno = deno * 10 + (expChrs[indx] - \'0\');\n                }\n\n                indx++;\n            }\n\n            if (deno == 0) deno = 1;\n            denoToSignNumMp.putIfAbsent(deno, new ArrayList<>());\n            denoToSignNumMp.get(deno).add(new int[]{num, sign});\n        }\n\n        int lcm = 1;\n        for (var deno : denoToSignNumMp.keySet()) {\n            lcm = getLcm(lcm, deno);\n        }\n\n        int totalNum = 0;\n        for (var entry : denoToSignNumMp.entrySet()) {\n            var deno = entry.getKey();\n            for (var numEntry : entry.getValue()) {\n                int num = numEntry[0], numSign = numEntry[1];\n                totalNum += (numSign * num * (lcm / deno));\n            }\n        }\n        int gcd = gcd(Math.abs(totalNum), lcm);\n        totalNum /= gcd;\n        lcm /= gcd;\n\n        if (lcm == 1) return totalNum + "/1";\n        return totalNum + "/" + lcm;\n    }\n\n    private int getLcm(int a, int b) {\n        return (a * b) / gcd(a, b);\n    }\n\n    private int gcd(int a, int b) {\n        while (b != 0) {\n            int temp = b;\n            b = a % b;\n            a = temp;\n        }\n        return a;\n    }\n}\n\n```\n```cpp []\nclass Solution {\npublic:\n    string fractionAddition(string expression) {\n        unordered_map<int, vector<pair<int, int>>> denoToSignNumMp;\n        int size = expression.length();\n        vector<char> expChrs(expression.begin(), expression.end());\n        int indx = 0;\n        int sign = 1;\n\n        while (indx < size) {\n            char currChar = expChrs[indx];\n            if (currChar == \'-\') {\n                sign = -1;\n                indx++;\n            } else if (currChar == \'+\') {\n                sign = 1;\n                indx++;\n            }\n\n            int num = 0, deno = 0;\n            bool slashFound = false;\n\n            while (indx < size && expChrs[indx] != \'+\' && expChrs[indx] != \'-\') {\n                if (expChrs[indx] == \'/\') {\n                    slashFound = true;\n                    indx++;\n                    continue;\n                }\n\n                if (!slashFound) {\n                    num = num * 10 + (expChrs[indx] - \'0\');\n                } else {\n                    deno = deno * 10 + (expChrs[indx] - \'0\');\n                }\n\n                indx++;\n            }\n\n            if (deno == 0) deno = 1;\n            denoToSignNumMp[deno].emplace_back(num, sign);\n        }\n\n        int lcm = 1;\n        for (const auto& denoEntry : denoToSignNumMp) {\n            lcm = getLcm(lcm, denoEntry.first);\n        }\n\n        int totalNum = 0;\n        for (const auto& entry : denoToSignNumMp) {\n            int deno = entry.first;\n            for (const auto& numEntry : entry.second) {\n                int num = numEntry.first, numSign = numEntry.second;\n                totalNum += (numSign * num * (lcm / deno));\n            }\n        }\n        int gcdValue = gcd(abs(totalNum), lcm);\n        totalNum /= gcdValue;\n        lcm /= gcdValue;\n\n        if (lcm == 1) return to_string(totalNum) + "/1";\n        return to_string(totalNum) + "/" + to_string(lcm);\n    }\n\nprivate:\n    int getLcm(int a, int b) {\n        return (a * b) / gcd(a, b);\n    }\n\n    int gcd(int a, int b) {\n        while (b != 0) {\n            int temp = b;\n            b = a % b;\n            a = temp;\n        }\n        return a;\n    }\n};\n```
['Math', 'String', 'Simulation', 'C++', 'Java']