input: 
  def optimize_equation(equation: str) -> str:
      """Given a string equation with terms like 'x' and 'y' each with a coefficient before it,
      optimize the equation by consolidating like terms and show each term in the form 'x^count'.
  
      >>> optimize_equation('x + x + y + y + y')
      'x^2 + y^3'
      >>> optimize_equation('a + b + b + a + a')
      'a^3 + b^2'
      """
      from collections import defaultdict
  
      variables = defaultdict(int)
      
      for term in equation.split('+'):
          var = term.strip()
          variables[var] += 1
      
      new_equation = ' + '.join(f"{var}^{count}" for var, count in variables.items())
      return new_equation
  
output:     from collections import defaultdict
  
      variables = defaultdict(int)
      
      for term in equation.split('+'):
          var = term.strip()
          variables[var] += 1
      
      new_equation = ' + '.join(f"{var}^{count}" for var, count in sorted(variables.items()))
      return new_equation