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