input: Write a function to optimize an equation by simplifying the terms. If a term occurs more than once, it should be combined in the form of var^count. If the string is empty, return an empty string.
  
  def optimize_equation(equation):
      if not equation:
          return ""
      variables = []
      for term in equation.split(' + '):
          var, count = term.strip(), equation.count(term)
          if (var, count) not in variables:
              variables.append((var, 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 + b') == 'a^2 + b^1'
  assert optimize_equation('') == ''
  assert optimize_equation('z + z + z + z + w + w') == 'z^4 + w^2'
  assert optimize_equation('p + q + p') == 'p^2 + q^1'
output: def optimize_equation(equation):
      if not equation:
          return ""
      terms = equation.split(' + ')
      term_count = {}
      for term in terms:
          term = term.strip()
          if term in term_count:
              term_count[term] += 1
          else:
              term_count[term] = 1
  
      new_equation = ""
      for term, count in term_count.items():
          new_equation += (term + '^' + str(count)) + ' + '
      return new_equation[:-3]