input: Write a function to optimize an equation by combining like terms and returning 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 + b + a + a + b') == 'a^3 + b^2'
  assert optimize_equation('z + z + z + z') == 'z^4'
  assert optimize_equation('') == ''
  assert optimize_equation('p + q') == 'p^1 + q^1'
output: def optimize_equation(equation):
      if not equation:
          return ''
      
      term_counts = {}
      for term in equation.split('+'):
          var, count = term.strip().split(' ')
          count = int(count)
          if var in term_counts:
              term_counts[var] += count
          else:
              term_counts[var] = count
  
      new_equation = ""
      for var, count in term_counts.items():
          new_equation += (var + '^' + str(count)) + ' + '
      
      return new_equation[:-3]