input: Write a python function to find the length of the longest substring with at most k distinct characters.
  assert longest_substring("araaci", 2) == 4
  assert longest_substring("araaci", 1) == 2
  assert longest_substring("cbbebi", 3) == 5
output: def longest_substring(s: str, k: int) -> int:
      if k == 0 or not s:
          return 0
      
      start = 0
      max_length = 0
      char_frequency = {}
      
      for end in range(len(s)):
          right_char = s[end]
          if right_char not in char_frequency:
              char_frequency[right_char] = 0
          char_frequency[right_char] += 1
          
          while len(char_frequency) > k:
              left_char = s[start]
              char_frequency[left_char] -= 1
              if char_frequency[left_char] == 0:
                  del char_frequency[left_char]
              start += 1
          
          max_length = max(max_length, end - start + 1)
      
      return max_length