Clean Code | Python, Java, C++ | Easily understandable and optimized codes.✅
Intuition and Approach\n Describe your first thoughts on how to solve this problem. \nThe problem is about evaluating a string expression containing fractions, 
# Intuition and Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about evaluating a string expression containing fractions, where each fraction is either added or subtracted. The key challenge is to correctly handle the addition and subtraction of fractions with different denominators and ensure the result is in its simplest form.\n\nIdentify Fractions: Parse the string to extract numerators and denominators of all fractions.\n\nFind a Common Denominator: To add or subtract fractions, convert them to have a common denominator.\n\nPerform the Operation: Add or subtract the numerators while keeping the common denominator.\n\nSimplify the Result: Reduce the resulting fraction to its simplest form by dividing both numerator and denominator by their greatest common divisor (GCD).\n\nReturn in Required Format: Ensure that the result is returned in the required format, even if it is a whole number.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n), where n is the length of the input string. Parsing and processing each fraction takes constant time.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1), as the space used is constant and does not grow with the input size.\n\n# Code\n```java []\nclass Solution {\n    public String fractionAddition(String expression) {\n\n        expression = expression.replace("-", "+-");\n        String[] fractions = expression.split("\\\\+");\n        \n        int numerator = 0, denominator = 1;\n        \n        for (String fraction : fractions) {\n            if (!fraction.isEmpty()) {\n                String[] parts = fraction.split("/");\n                int num = Integer.parseInt(parts[0]);\n                int den = Integer.parseInt(parts[1]);\n                \n                numerator = numerator * den + num * denominator;\n                denominator *= den;\n                \n                int gcd = gcd(Math.abs(numerator), Math.abs(denominator));\n                numerator /= gcd;\n                denominator /= gcd;\n            }\n        }\n        \n        return numerator + "/" + denominator;\n    }\n    \n    private static 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# Code\n```Python []\nimport math\nfrom fractions import Fraction\n\ndef fractionAddition(expression: str) -> str:\n    # Replace \'-\' with \'+-\' to split easily\n    expression = expression.replace(\'-\', \'+-\')\n    fractions = expression.split(\'+\')\n\n    # Initialize result as 0/1\n    result = Fraction(0, 1)\n\n    for fraction in fractions:\n        if fraction:\n            result += Fraction(fraction)\n    \n    # Convert the result to a string in numerator/denominator format\n    return f"{result.numerator}/{result.denominator}"\n```\n# Code\n```C++ []\n#include <iostream>\n#include <string>\n#include <sstream>\n#include <numeric>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    string fractionAddition(string expression) {\n        expression = replace(expression, "-", "+-");\n        stringstream ss(expression);\n        string fraction;\n        int numerator = 0, denominator = 1;\n        \n        while (getline(ss, fraction, \'+\')) {\n            if (!fraction.empty()) {\n                int num, den;\n                sscanf(fraction.c_str(), "%d/%d", &num, &den);\n                \n                numerator = numerator * den + num * denominator;\n                denominator *= den;\n                \n                int gcd_val = gcd(abs(numerator), abs(denominator));\n                numerator /= gcd_val;\n                denominator /= gcd_val;\n            }\n        }\n        \n        return to_string(numerator) + "/" + to_string(denominator);\n    }\n    \nprivate:\n    string replace(string str, string from, string to) {\n        size_t pos = 0;\n        while ((pos = str.find(from, pos)) != string::npos) {\n            str.replace(pos, from.length(), to);\n            pos += to.length();\n        }\n        return str;\n    }\n\n    int gcd(int a, int b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n};\n```\n\n\n
['Java']