The problem. Given a string s of lowercase letters and a number k, return the length of the longest substring with exactly k distinct characters. If no substring has exactly k, return −1.
s = "aabacbebebe", k = 3 -> 7 ("cbebebe")
s = "aaaa", k = 2 -> -1 (only one kind of letter)
s = "aabbcc", k = 2 -> 4 ("aabb" or "bbcc")Members only · free account
The rest of this lesson, with 6 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson