Python | Greedy
see the Successfully Accepted Submission\n\n# Code\npython3 []\nimport re\nfrom math import gcd\n\nclass Solution:\n    def fractionAddition(self, expression: s
[see the Successfully Accepted Submission](https://leetcode.com/problems/fraction-addition-and-subtraction/submissions/1365428117/?envType=daily-question&envId=2024-08-23)\n\n# Code\n```python3 []\nimport re\nfrom math import gcd\n\nclass Solution:\n    def fractionAddition(self, expression: str) -> str:\n        # Find all the fractions in the expression, including signs\n        fractions = re.findall(\'[+-]?\\\\d+/\\\\d+\', expression)\n        \n        # Initialize numerator and denominator of the result\n        numerator = 0\n        denominator = 1\n        \n        # Process each fraction\n        for fraction in fractions:\n            # Extract numerator and denominator\n            num, denom = map(int, fraction.split(\'/\'))\n            \n            # Calculate the new numerator and denominator for the result\n            numerator = numerator * denom + num * denominator\n            denominator *= denom\n        \n        # Calculate GCD to simplify the fraction\n        common_divisor = gcd(abs(numerator), denominator)\n        \n        # Simplify the fraction\n        numerator //= common_divisor\n        denominator //= common_divisor\n        \n        # Return the result as a string\n        return f"{numerator}/{denominator}"\n\n\n```\n\n# Steps to Solve the Problem\n\n1. **Parse the Input String**: Extract all the fractions from the expression string.\n2. **Calculate the Least Common Denominator (LCD)**: For all the denominators in the expression, calculate the LCD so that we can easily add or subtract the fractions.\n3. **Convert Fractions to Have the Same Denominator**: Use the LCD to convert all fractions to have the same denominator.\n4. **Sum the Numerators**: Add or subtract the numerators of these fractions.\n5. **Simplify the Result**: Reduce the resulting fraction to its simplest form using the greatest common divisor (GCD).\n6. **Return the Result**: Ensure the result is in the correct format.\n\n# Explanation\n\n## Parsing Fractions\n\n- We use the regex pattern `\'[+-]?\\\\d+/\\\\d+\'` to match fractions including their signs.\n\n## Addition/Subtraction of Fractions\n\n- For each fraction, we update the overall numerator using the formula:\n  \n  \\[\n  \\text{{new\\_numerator}} = \\text{{current\\_numerator}} \\times \\text{{denom}} + \\text{{num}} \\times \\text{{current\\_denominator}}\n  \\]\n\n- The denominator is multiplied directly since we are summing fractions.\n\n## Simplifying the Result\n\n- We calculate the greatest common divisor (GCD) of the numerator and denominator to reduce the fraction.\n\n## Handling Edge Cases\n\n- The result automatically handles edge cases, such as resulting in a fraction of `0` (e.g., `"0/1"`).\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']