input: Write a function to optimize an equation by combining like terms and simplifying the expression. The function should return the simplified equation.
  
  def optimize_equation(equation):
      variables = []
      for term in equation.split('+'):
          var, count = term.strip().split(' ')
          variables.append((var, int(count)))
  
      new_equation = ""
      for var, count in variables:
          new_equation += (var + '^' + str(count)) + ' + '
      return new_equation[:-3]
  
  assert optimize_equation('x + x + y + y + y') == 'x^2 + y^3'
  assert optimize_equation('a + a + a + b + b') == 'a^3 + b^2'
  assert optimize_equation('c + c + c + c') == 'c^4'
  assert optimize_equation('d + e + d + e') == 'd^2 + e^2'
  assert optimize_equation('f + g + g + f') == 'f^2 + g^2'
output: def optimize_equation(equation):
      from collections import defaultdict
      variables = defaultdict(int)
      for term in equation.split('+'):
          var, count = term.strip().split(' ')
          variables[var] += int(count)
  
      new_equation = ""
      for var, count in variables.items():
          new_equation += (var + '^' + str(count)) + ' + '
      return new_equation[:-3]