Problem
Given a string, find the longest substring that contains only k
unique or distinct characters.
Examples
Example 1:
|
|
Example 2:
|
|
Solutions
Method 1 - Brute Force
If the length of string is n, then there can be n*(n+1)/2
possible substrings. A simple way is to generate all the substring and check each one whether it has exactly k unique characters or not. If we apply this brute force, it would take O(n^2) to generate all substrings and O(n) to do a check on each one. Thus overall it would go O(n^3).
We can further improve this solution by creating a hash table and while generating the substrings, check the number of unique characters using that hash table. Thus it would improve up to O(n^2).
Method 2 - Sliding Window with Frequency Hashmap
This problem is based on the variable-size sliding window pattern. We have already solved a similar problem Find Smallest Subarray with a given sum K greater than or equal using this pattern. We will use the same strategy to solve this problem as well.
- Start with a sliding window of size
1
(left=0, right=0
). We expand window with right, and shrink it by moving left - Use a map to store frequency for each character in the current window.
- Iterate over the string and insert new characters in the HashMap until we have exactly
K
distinct characters in the HashMap.- Iterate through the string, expanding the window by adding characters until we have exactly K distinct characters in the HashMap (representing the current longest substring with K unique characters).
- If the window contains more than K distinct characters, shrink it from the left by removing the leftmost element from the HashMap, continuing until we have K or fewer distinct characters.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n)
- 🧺 Space complexity:
O(n)