input: Write a function to optimize an equation by combining like terms and representing the counts as exponents. The function should take a string representing an equation as input and return a simplified equation as a string. If the input equation is empty, the function should return an empty string.
  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('')==''
  assert optimize_equation('w + z + w + w')=='w^3 + z'
  assert optimize_equation('p + q + q + p + q')=='p^2 + q^3'
output: def optimize_equation(equation):
      if not equation:
          return ''
      terms = equation.split(' + ')
      counts = {}
      for term in terms:
          if term in counts:
              counts[term] += 1
          else:
              counts[term] = 1
      result = ' + '.join(f'{term}^{counts[term]}' if counts[term] > 1 else term for term in sorted(counts))
      return result