Efficient way to solve the problem using Regex pattern match
Intuition\nTo solve fraction addition and subtraction, convert all fractions to a common denominator, sum their numerators, and reduce the result to its simples
# Intuition\nTo solve fraction addition and subtraction, convert all fractions to a common denominator, sum their numerators, and reduce the result to its simplest form. If the result is an integer, express it as a fraction with denominator 1 (e.g., 2/1). This ensures the arithmetic is accurate and the output is in its most simplified form.\n\n# Approach\n# 1. **Parse the Expression:**\n- **Identify Fractions:** Split the string into individual fractions and signs using a regular expression. This ensures we correctly handle both positive and negative fractions. You can use a regex pattern like (?=[+-]) to separate the fractions based on the signs.\n- **Handle Mixed Signs:** Ensure that the parsing captures both positive and negative signs for each fraction.\n# 2. Normalize Fractions:\n- **Common Denominator:** To add or subtract fractions, they need to have the same denominator. The least common denominator (LCD) of the fractions can be used for this purpose.\n- **Convert Each Fraction:** Convert each fraction to have this common denominator, adjusting the numerators accordingly.\n# 3. Perform Addition/Subtraction:\n- **Sum the Numerators:** After normalizing the fractions, add or subtract the numerators based on the sign. The denominator remains the common denominator.\n- **Simplify the Result:** The final fraction may need to be reduced to its irreducible form by dividing both the numerator and denominator by their greatest common divisor (GCD).\n# 4. Format the Result:\n- **Check for Integer Result:** If the numerator is divisible by the denominator, return the result as an integer over 1 (e.g., 2/1).\nOtherwise: Return the fraction in its reduced form.\n# Complexity\n- Time complexity:O(N)\n\n- Space complexity:O(N)\n\n# Code\n```java []\nclass Solution {\n    public int gcd(int a, int b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n    public int lcm(int a, int b) {\n        return (a / gcd(a, b)) * b;\n    }\n\n    public String fractionAddition(String expression) {\n        String[] sep = expression.split("(?=[+-])|/");\n        if (sep.length == 2) return expression;\n\n        int[] parts = new int[sep.length];\n        for (int i = 0; i < sep.length; i++) {\n            parts[i] = Integer.parseInt(sep[i]);\n        }\n        int lcm = parts[1];\n        for (int i = 3; i < parts.length; i += 2) {\n            lcm = lcm(lcm, parts[i]);\n        }\n        int sum = 0;\n        for (int i = 1; i < parts.length; i += 2) {\n            sum += parts[i - 1] * (lcm / parts[i]);\n        }\n        int gcd = gcd(Math.abs(sum), lcm);\n        return (sum / gcd) + "/" + (lcm / gcd);\n    }\n}\n\n```
['Java']