Given an integer k and a string s, find the length of the longest substring that contains at most k distinct characters
Necessary imports are missing.
The prefix is discarded too aggressively. In a string abaacccc the entire abaa is discarded, and the result becomes 4 instead of 6.
Representing window as a StringBuilder seems excessive. A pair of indices into the original string is enough.