Introduction
Finding substrings with unique characters is a common problem in string manipulation and interview questions.
GFG’s Longest Substring with K Unique Characters problem challenges you to find the length of the longest substring containing exactly K distinct characters.
The focus is not just on brute-force solutions, but on using sliding window with a HashMap to efficiently track character frequencies and window boundaries.
If you’d like to try solving the problem first, you can attempt it here:
Try the problem on GeeksforGeeks:
https://leetcode.com/problems/max-consecutive-ones-iii/
Problem Understanding
You are given:
- A string
sof lowercase letters - An integer
k
Your task: Return the length of the longest substring containing exactly k distinct characters.
Examples:
A naive approach would try all possible substrings, count unique characters, and track the max length.
- Time Complexity: O(n²)
- Inefficient for large strings
Key Idea: Sliding Window + HashMap
Instead of recomputing the unique characters for every substring:
- Use a HashMap to store character frequencies in the current window
- Use two pointers (
iandj) to maintain the window - Expand the window by moving
jand add characters to the map - Shrink the window from the left when the number of unique characters exceeds
k - Update the max length whenever the window has exactly
kunique characters
This ensures each character is processed only once, giving a linear solution.
Approach (Step-by-Step)
- Initialize a HashMap
mpto store character counts - Use two pointers
i(window start) andj(window end) - Iterate over the string with
j - Add
s[j]to the map and update its count - Check the map size:
- If size < k → move
jforward - If size == k → update
maxlength, movejforward - If size > k → shrink window from
iuntil map size <= k - After iterating, return
max(or -1 if no valid substring exists)
Optimization:
- Using a HashMap avoids recalculating the number of unique characters for every substring
- Sliding window ensures O(n) complexity
Implementation (Java)
Dry Run Example
Input:
Execution:
- Window
[0-4] = "aaba"→ unique = 2 → continue - Window
[1-6] = "abacb"→ unique = 3 → max = 5 - Window
[4-10] = "cbebebe"→ unique = 3 → max = 7
Output:
Complexity Analysis
- Time Complexity: O(n) → Each character enters and leaves the window once
- Space Complexity: O(k) → HashMap stores at most
kunique characters
Edge Cases Considered
kgreater than number of unique characters → return -1- Entire string has exactly
kunique characters - String with repeated characters only
- Minimum string length (1)
Sliding Window Pattern Importance
This problem reinforces a common sliding window + hashmap pattern used in:
- Longest substring problems with constraints
- Counting substrings with exactly K conditions
- Optimizing brute-force approaches to linear solutions
Conclusion
By combining sliding window with HashMap frequency tracking, we reduce an O(n²) problem to O(n).
Once you master this approach, many string and substring problems with constraints on unique characters become much easier to solve efficiently.




