code: def longest_substring_with_distinct_characters(s, k):
      letters = {}
      longest = 0
      length = 0
  
      for i in range(len(s)):
          if s[i] in letters:
              # if this letter exists then only increase the counter and length
              letters[s[i]] += 1
              length += 1
          else:
              # if this letter doesn't exist then remove all distinct letters from the front
              # so the count of distinct letters will be k-1
              while len(letters) == k:
                  firstLetter = s[i - length]
                  letters[firstLetter] -= 1  # decrease the counter
                  if letters[firstLetter] == 0:
                      # remove this letter from the dictionary because
                      # in the susbtring there are no letters like this
                      del letters[firstLetter]
                  length -= 1
  
              # add the new letter in the dictionary
              letters[s[i]] = 1
              length += 1
  
          # check if this length is the longest one
          longest = max(longest, length)
  
      return longest
  
label_desc: Use image node_id 1 for calling a global function with example usage: longest_substring_with_distinct_characters(s, k) and returns: longest