input: def optimize_equation(equation: str) -> str:
      """Optimize an equation by collecting like terms and raising them to their powers.
      >>> optimize_equation('x + x + y + y + y')
      'x^2 + y^3'
      """
output:     
      terms = {}
      for term in equation.split(' + '):
          base, _, exponent = term.rpartition('^')
          if exponent:
              exponent = int(exponent)
          else:
              exponent = 1
          if base in terms:
              terms[base] += exponent
          else:
              terms[base] = exponent
      
      optimized_terms = [f"{base}^{exponent}" if exponent > 1 else base for base, exponent in terms.items()]
      return ' + '.join(optimized_terms)
  