Shortest Substring with at Least K Distinct Characters
Problem statement
Return the minimum length of a contiguous substring of text that contains at least k distinct characters. Return -1 if no such substring exists.
Function
shortestAtLeastKDistinct(text: String, k: int) → intExamples
Example 1
text = "aabcbcdbca"k = 3return = 3The substring abc has three distinct characters.
Example 2
text = "aaaa"k = 2return = -1Only one distinct character exists.
Constraints
1 <= text.length <= 200000.1 <= k <= 256.- For this exercise, assume
textcontains only ASCII characters; one character is one byte.