input: Write a function to optimize an equation by combining like terms into exponential notation. The function should take a string representing the equation as input and return the optimized equation in a string format.
  assert optimize_equation('x + x + y + y + y')=='x^2 + y^3'
  assert optimize_equation('a + a + a + b + c + c')=='a^3 + b + c^2'
  assert optimize_equation('p + p + p + p + q + q + q + q + q + q')=='p^4 + q^6'
  assert optimize_equation('d + d')=='d^2'
  assert optimize_equation('e + e + f')=='e^2 + f'
output: def optimize_equation(equation):
      from collections import Counter
      terms = equation.split(' + ')
      term_counts = Counter(terms)
      optimized_terms = [f"{term}^{count}" if count > 1 else term for term, count in term_counts.items()]
      return ' + '.join(sorted(optimized_terms))