input: 
  def optimize_equation(equation: str) -> str:
      """ Optimize the given equation by reducing duplicate variables and their counts.
      For example, the equation 'x + x + y + y + y' should be optimized to 'x^2 + y^3'.
      >>> optimize_equation('x + x + y + y + y')
      'x^2 + y^3'
      >>> optimize_equation('z + z + z + z')
      'z^4'
      """
  
output:     from collections import Counter
      
      # Split the equation into individual terms
      terms = equation.split(' + ')
      
      # Count the occurrences of each term
      term_counts = Counter(terms)
      
      # Create the optimized equation
      optimized_terms = [f"{term}^{count}" if count > 1 else term for term, count in term_counts.items()]
      
      # Join the optimized terms with ' + '
      optimized_equation = ' + '.join(optimized_terms)
      
      return optimized_equation