input: def optimize_equation(equation: str) -> str:
      """ Optimize an equation by combining like terms in the format "variable^count".
      >>> optimize_equation('x + x + y + y + y')
      'x^2 + y^3'
      >>> optimize_equation('a + b + b + a')
      'a^2 + b^2'
      """
      from collections import defaultdict
  
      terms = defaultdict(int)
      for term in equation.split('+'):
          var = term.strip()
          terms[var] += 1
  
      new_equation = []
      for var, count in terms.items():
          new_equation.append(f"{var}^{count}")
  
      return ' + '.join(new_equation)
output:     from collections import defaultdict
  
      terms = defaultdict(int)
      for term in equation.split('+'):
          var = term.strip()
          terms[var] += 1
  
      new_equation = []
      for var, count in terms.items():
          new_equation.append(f"{var}^{count}")
  
      return ' + '.join(new_equation)