Java Solution for Fraction Addition and Subtraction Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem involves adding and subtracting fractions, which requires finding a common 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem involves adding and subtracting fractions, which requires finding a common denominator and combining the numerators accordingly. The key steps involve parsing the input expression, extracting the numerators and denominators, and then performing the arithmetic operations. The result must be returned as an irreducible fraction.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tInitialization:\n\t\u2022\tStart with an initial fraction of 0/1.\n2.\tParsing:\n\t\u2022\tIterate through the expression, extracting each fraction by identifying the numerator and denominator. Handle signs (+ or -) appropriately to ensure the correct operation (addition or subtraction).\n3.\tCombining Fractions:\n\t\u2022\tFor each fraction extracted, update the overall result by:\n\t    \u2022\tFinding a common denominator between the current fraction and the result.\n\t    \u2022\tAdjusting the numerators accordingly and summing them up.\n\t\u2022\tSimplify the fraction after each operation by dividing the numerator and denominator by their greatest common divisor (GCD).\n4.\tReturning the Result:\n\t\u2022\tOnce all fractions have been processed, return the final result as a string in the format numerator/denominator.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(n): The time complexity is linear relative to the length of the input string. Each character is processed exactly once.\n\u2022\tO(1): The GCD computation for each fraction addition takes constant time since the numerator and denominator values are bounded by small integers.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\u2022\tO(1): The space complexity is constant because the solution uses a fixed amount of extra space for variables (numerator, denominator, and gcd). No additional space scales with the input size.\n\n# Code\n```java []\nclass Solution {\n    public String fractionAddition(String expression) {\n        int numerator = 0, denominator = 1;\n        int i = 0, n = expression.length();\n\n        while (i < n) {\n            int sign = 1;\n            if (expression.charAt(i) == \'+\' || expression.charAt(i) == \'-\') {\n                sign = expression.charAt(i) == \'-\' ? -1 : 1;\n                i++;\n            }\n\n            int num = 0;\n            while (i < n && Character.isDigit(expression.charAt(i))) {\n                num = num * 10 + (expression.charAt(i) - \'0\');\n                i++;\n            }\n\n            num *= sign;\n\n            i++; // Skip the \'/\'\n            \n            int denom = 0;\n            while (i < n && Character.isDigit(expression.charAt(i))) {\n                denom = denom * 10 + (expression.charAt(i) - \'0\');\n                i++;\n            }\n\n            numerator = numerator * denom + num * denominator;\n            denominator *= denom;\n\n            int gcd = gcd(Math.abs(numerator), denominator);\n            numerator /= gcd;\n            denominator /= gcd;\n        }\n\n        return numerator + "/" + denominator;\n    }\n\n    private int gcd(int a, int b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n\n}\n```
['Java']