While loop + Build a Parser (explanation + code comments)
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can parse the string with a while loop:\n1. Before we start the while loop, get the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can parse the string with a while loop:\n1. Before we start the while loop, get the first fraction. This will be the overall fraction that we will add to / subtract from\n2. Keep looping as we get a new operand and next fraction\n3. Combine and simplify the fraction\n\n# Complexity\n- Time complexity: O(N)\nWe parse through the string once and perform add/subtract for each operation.\n\n- Space complexity: O(N) or O(1)\nThe answer can be just as long as the input (or half as long)\nEx: 1/2 - 1/4 = -1/8\nBut in this case, the numerator / denominator are <= 10 so the answer is going to remain small ~ O(1). We are also simplifying the fraction at each operation (rather than at the end), so the denominator won\'t get absurdly large.\n\n# Code\n```python3 []\nclass Solution:\n\n    def __init__(self):\n        self.operators = ["+","-"]\n        self.idx = 0\n\n    def getNumerator(self, expression) -> str:\n        numerator = ""\n        start = self.idx\n        while not(expression[self.idx] == "/"):\n            self.idx += 1\n        numerator = expression[start:self.idx]\n        return numerator\n    \n    def getDenominator(self, expression) -> str:\n        denominator = ""\n        start = self.idx\n        while (self.idx < len(expression)) and not((expression[self.idx] in self.operators)):\n            self.idx += 1\n        denominator = expression[start:self.idx]\n        return denominator\n\n    # Returns the next fraction in form of (numerator, denominator)\n    def getFraction(self, expression) -> (int,int):\n        numerator = int(self.getNumerator(expression))\n        self.idx += 1 # Go pass the "/"\n        denominator = int(self.getDenominator(expression))\n        return (numerator, denominator)\n\n\n    def combineFract(self, fraction1, fraction2, operator) -> (int,int):\n        num1,den1 = fraction1[0],fraction1[1]\n        num2,den2 = fraction2[0],fraction2[1]\n        \n        # Get a common denominator so we can combine them\n        commonDenom = den1 * den2\n        num1 = num1 * den2\n        num2 = num2 * den1\n\n        if operator == "+":\n            numerator = num1 + num2\n        else:\n            numerator = num1 - num2\n        \n        # Simplify the fraction\n        greatestCommonDivisor = gcd(abs(numerator), commonDenom)\n        numerator = numerator // greatestCommonDivisor\n        denominator = commonDenom // greatestCommonDivisor\n\n        return (numerator, denominator)\n\n    def fractionAddition(self, expression: str) -> str:\n        lenExpression = len(expression)\n        \n        # Get first fraction\n        fraction = self.getFraction(expression)\n\n        while True:\n            # If there are no new operators, don\'t need to do anymore add/subtract\n            if self.idx >= len(expression):\n                break\n\n            # Get operator\n            operator = expression[self.idx]\n            self.idx += 1\n\n            # Get next fraction\n            nextFraction = self.getFraction(expression)\n\n            # Combine them\n            fraction = self.combineFract(fraction,nextFraction,operator)\n        \n        return str(fraction[0]) + "/" + str(fraction[1])\n```
['Python3']