Efficient Fraction Addition and Subtraction Using Python's Fraction Class
Intuition\nAdding and subtracting fractions involves two main steps: figuring out the individual fractions (and their signs) from the input string, and then doi
# Intuition\nAdding and subtracting fractions involves two main steps: figuring out the individual fractions (and their signs) from the input string, and then doing the math to add or subtract these fractions.\n\nThe tricky part comes when you\'re working with fractions that have different bottom numbers. You can\'t just add or subtract fractions with different bottom numbers without first making them have the same bottom number. This is the heart of the problem: you need to follow the rules for doing math with fractions.\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1.Parsing the Input String\n2.Using the Fraction Class\n3.Simplifying the Result\n4.Returning the Result\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nfrom fractions import Fraction\nclass Solution:\n    def fractionAddition(self, e: str) -> str:\n        e=e.replace(\'-\',\'+-\')\n        a=e.split(\'+\')\n        a=[i for i in a if i]\n        result = Fraction(0, 1)\n        for i in a:\n            result += Fraction(i)\n        return f"{result.numerator}/{result.denominator}"     \n```
['Math', 'Python3']