Problem Description
Given a string s and an integer k, the task is to determine the length of the longest substring of s that contains at most k distinct characters.
Key Insights
- Use the sliding window technique to efficiently scan through the string.
- Maintain a hash table (or dictionary) that keeps track of the frequency of characters in the current window.
- When the number of distinct characters exceeds k, increment the left pointer to shrink the window until the condition is met.
- Update the maximum length of valid substrings throughout the process.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string, as each character is processed at most twice (once when expanding the window and once when contracting it).
Space Complexity: O(min(m, k)), where m is the size of the charset (or number of unique characters in s), since at most k+1 keys are stored.
Solution
This problem is solved using the sliding window technique. The algorithm keeps track of a window defined by two indices (left and right pointers). A hash map is used to count the frequency of each character in the current window. As the right pointer extends the window, the count of the character is incremented. When the count of distinct keys in the map exceeds k, the left pointer is moved forward while decrementing the count of the leftmost character, until the condition is restored. The maximum window length encountered is recorded and returned as the result.