input: Write a python function to count substrings with k distinct characters.
  assert count_substrings_with_k_distinct_characters("abcabcabcabc", 2) == 10
  assert count_substrings_with_k_distinct_characters("aaabbbccc", 2) == 0
  assert count_substrings_with_k_distinct_characters("abcd", 3) == 2
output: def count_substrings_with_k_distinct_characters(s, k):
      from collections import defaultdict
  
      def at_most_k_distinct(s, k):
          count = defaultdict(int)
          left = 0
          total = 0
          for right in range(len(s)):
              count[s[right]] += 1
              while len(count) > k:
                  count[s[left]] -= 1
                  if count[s[left]] == 0:
                      del count[s[left]]
                  left += 1
              total += right - left + 1
          return total
  
      return at_most_k_distinct(s, k) - at_most_k_distinct(s, k - 1)