Before attempting this problem, you should be comfortable with:
Sliding Window Technique - Essential for efficiently finding the longest valid substring without rechecking overlapping portions
Hash Map / Dictionary - Used to track character frequencies within the current window and count distinct characters
Two Pointers - The sliding window is implemented using left and right pointers to define window boundaries
1. Brute Force
Intuition
The most direct approach is to examine every possible substring and check if it contains at most two distinct characters. For each starting position, we extend the substring character by character, tracking the distinct characters seen. Once we see a third distinct character, we stop and record the maximum valid length found.
Algorithm
Initialize res = 0 to store the maximum length.
For each starting index i from 0 to n-1:
Create a set seen to track distinct characters.
Initialize curLen = 0.
For each ending index j from i to n-1:
Add s[j] to the set.
If the set size exceeds 2, break out of the inner loop.
classSolution:deflengthOfLongestSubstringTwoDistinct(self, s:str)->int:
res, n =0,len(s)for i inrange(n):
seen =set()
cnt = curLen =0for j inrange(i, n):
seen.add(s[j])iflen(seen)>2:break
curLen +=1
res =max(res, curLen)return res
publicclassSolution{publicintlengthOfLongestSubstringTwoDistinct(String s){int res =0, n = s.length();for(int i =0; i < n; i++){Set<Character> seen =newHashSet<>();int curLen =0;for(int j = i; j < n; j++){
seen.add(s.charAt(j));if(seen.size()>2){break;}
curLen++;}
res =Math.max(res, curLen);}return res;}}
classSolution{public:intlengthOfLongestSubstringTwoDistinct(string s){int res =0, n = s.size();for(int i =0; i < n; i++){
unordered_set<char> seen;int curLen =0;for(int j = i; j < n; j++){
seen.insert(s[j]);if(seen.size()>2){break;}
curLen++;}
res =max(res, curLen);}return res;}};
classSolution{/**
* @param {string} s
* @return {number}
*/lengthOfLongestSubstringTwoDistinct(s){let res =0, n = s.length;for(let i =0; i < n; i++){let seen =newSet();let curLen =0;for(let j = i; j < n; j++){
seen.add(s[j]);if(seen.size >2){break;}
curLen++;}
res = Math.max(res, curLen);}return res;}}
publicclassSolution{publicintLengthOfLongestSubstringTwoDistinct(string s){int res =0, n = s.Length;for(int i =0; i < n; i++){HashSet<char> seen =newHashSet<char>();int curLen =0;for(int j = i; j < n; j++){
seen.Add(s[j]);if(seen.Count >2){break;}
curLen++;}
res = Math.Max(res, curLen);}return res;}}
funclengthOfLongestSubstringTwoDistinct(s string)int{
res, n :=0,len(s)for i :=0; i < n; i++{
seen :=make(map[byte]bool)
curLen :=0for j := i; j < n; j++{
seen[s[j]]=trueiflen(seen)>2{break}
curLen++}if curLen > res {
res = curLen
}}return res
}
class Solution {funlengthOfLongestSubstringTwoDistinct(s: String): Int {var res =0val n = s.length
for(i in0 until n){val seen = HashSet<Char>()var curLen =0for(j in i until n){
seen.add(s[j])if(seen.size >2){break}
curLen++}
res =maxOf(res, curLen)}return res
}}
classSolution{funclengthOfLongestSubstringTwoDistinct(_ s:String)->Int{let chars =Array(s)var res =0let n = chars.count
for i in0..<n {var seen =Set<Character>()var curLen =0for j in i..<n {
seen.insert(chars[j])if seen.count >2{break}
curLen +=1}
res =max(res, curLen)}return res
}}
Time & Space Complexity
Time complexity: O(n2)
Space complexity: O(1) since we have at most 52 different characters.
2. Sliding Window
Intuition
We maintain a window that always contains at most two distinct characters. As we expand the window to the right, we track character frequencies in a hash map. When adding a new character causes us to have more than two distinct characters, we shrink the window from the left until we're back to two or fewer distinct characters.
Algorithm
Initialize res = 0, j = 0 (left pointer), and a hash map seen for character counts.
For each i (right pointer) from 0 to n-1:
Increment the count of s[i] in the map.
While the map contains more than 2 distinct characters:
classSolution:deflengthOfLongestSubstringTwoDistinct(self, s:str)->int:
res, n =0,len(s)
seen = defaultdict(int)
j =0for i inrange(n):
seen[s[i]]+=1whilelen(seen)>2:
seen[s[j]]-=1if seen[s[j]]==0:
seen.pop(s[j])
j +=1
res =max(res, i - j +1)return res
publicclassSolution{publicintlengthOfLongestSubstringTwoDistinct(String s){int res =0, n = s.length();Map<Character,Integer> seen =newHashMap<>();int j =0;for(int i =0; i < n; i++){
seen.put(s.charAt(i), seen.getOrDefault(s.charAt(i),0)+1);while(seen.size()>2){char c = s.charAt(j);
seen.put(c, seen.get(c)-1);if(seen.get(c)==0){
seen.remove(c);}
j++;}
res =Math.max(res, i - j +1);}return res;}}
classSolution{public:intlengthOfLongestSubstringTwoDistinct(string s){int res =0, n = s.size();
unordered_map<char,int> seen;int j =0;for(int i =0; i < n; i++){
seen[s[i]]++;while(seen.size()>2){char c = s[j];
seen[c]--;if(seen[c]==0){
seen.erase(c);}
j++;}
res =max(res, i - j +1);}return res;}};
classSolution{/**
* @param {string} s
* @return {number}
*/lengthOfLongestSubstringTwoDistinct(s){let res =0, n = s.length;let seen =newMap();let j =0;for(let i =0; i < n; i++){
seen.set(s[i],(seen.get(s[i])||0)+1);while(seen.size >2){let c = s[j];
seen.set(c, seen.get(c)-1);if(seen.get(c)===0){
seen.delete(c);}
j++;}
res = Math.max(res, i - j +1);}return res;}}
publicclassSolution{publicintLengthOfLongestSubstringTwoDistinct(string s){int res =0, n = s.Length;Dictionary<char,int> seen =newDictionary<char,int>();int j =0;for(int i =0; i < n; i++){if(!seen.ContainsKey(s[i])){
seen[s[i]]=0;}
seen[s[i]]++;while(seen.Count >2){char c = s[j];
seen[c]--;if(seen[c]==0){
seen.Remove(c);}
j++;}
res = Math.Max(res, i - j +1);}return res;}}
funclengthOfLongestSubstringTwoDistinct(s string)int{
res, n :=0,len(s)
seen :=make(map[byte]int)
j :=0for i :=0; i < n; i++{
seen[s[i]]++forlen(seen)>2{
c := s[j]
seen[c]--if seen[c]==0{delete(seen, c)}
j++}if i-j+1> res {
res = i - j +1}}return res
}
class Solution {funlengthOfLongestSubstringTwoDistinct(s: String): Int {var res =0val n = s.length
val seen = HashMap<Char, Int>()var j =0for(i in0 until n){
seen[s[i]]= seen.getOrDefault(s[i],0)+1while(seen.size >2){val c = s[j]
seen[c]= seen[c]!!-1if(seen[c]==0){
seen.remove(c)}
j++}
res =maxOf(res, i - j +1)}return res
}}
classSolution{funclengthOfLongestSubstringTwoDistinct(_ s:String)->Int{let chars =Array(s)var res =0let n = chars.count
var seen =[Character:Int]()var j =0for i in0..<n {
seen[chars[i],default:0]+=1while seen.count >2{let c = chars[j]
seen[c]!-=1if seen[c]==0{
seen.removeValue(forKey: c)}
j +=1}
res =max(res, i - j +1)}return res
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) since we have at most 52 different characters.
3. Sliding Window (Optimal)
Intuition
We can further optimize by observing that we only care about the maximum window size. Instead of tracking the actual maximum during iteration, we maintain a window that never shrinks by more than one element at a time. When we have too many distinct characters, we shift the entire window right by one. The final window size represents the longest valid substring.
Algorithm
Initialize j = 0 (left pointer) and a hash map count for character frequencies.
classSolution:deflengthOfLongestSubstringTwoDistinct(self, s:str)->int:
n =len(s)
count = defaultdict(int)
j =0for i inrange(n):
count[s[i]]+=1iflen(count)>2:
count[s[j]]-=1if count[s[j]]==0:
count.pop(s[j])
j +=1return i - j +1
publicclassSolution{publicintlengthOfLongestSubstringTwoDistinct(String s){int n = s.length();Map<Character,Integer> count =newHashMap<>();int j =0, i =0;for(i =0; i < n; i++){
count.put(s.charAt(i), count.getOrDefault(s.charAt(i),0)+1);if(count.size()>2){char c = s.charAt(j);
count.put(c, count.get(c)-1);if(count.get(c)==0) count.remove(c);
j++;}}return i - j;}}
classSolution{public:intlengthOfLongestSubstringTwoDistinct(string s){int n = s.size();
unordered_map<char,int> count;int j =0, i =0;for(i =0; i < n; i++){
count[s[i]]++;if(count.size()>2){
count[s[j]]--;if(count[s[j]]==0) count.erase(s[j]);
j++;}}return i - j;}};
classSolution{/**
* @param {string} s
* @return {number}
*/lengthOfLongestSubstringTwoDistinct(s){let n = s.length;let count =newMap();let j =0, i =0;for(i =0; i < n; i++){
count.set(s[i],(count.get(s[i])||0)+1);if(count.size >2){
count.set(s[j], count.get(s[j])-1);if(count.get(s[j])===0) count.delete(s[j]);
j++;}}return i - j;}}
publicclassSolution{publicintLengthOfLongestSubstringTwoDistinct(string s){int n = s.Length;Dictionary<char,int> count =newDictionary<char,int>();int j =0, i =0;for(i =0; i < n; i++){if(!count.ContainsKey(s[i])) count[s[i]]=0;
count[s[i]]++;if(count.Count >2){
count[s[j]]--;if(count[s[j]]==0) count.Remove(s[j]);
j++;}}return i - j;}}
funclengthOfLongestSubstringTwoDistinct(s string)int{
n :=len(s)
count :=make(map[byte]int)
j :=0for i :=0; i < n; i++{
count[s[i]]++iflen(count)>2{
count[s[j]]--if count[s[j]]==0{delete(count, s[j])}
j++}}return n - j
}
class Solution {funlengthOfLongestSubstringTwoDistinct(s: String): Int {val n = s.length
val count = HashMap<Char, Int>()var j =0for(i in0 until n){
count[s[i]]= count.getOrDefault(s[i],0)+1if(count.size >2){
count[s[j]]= count[s[j]]!!-1if(count[s[j]]==0) count.remove(s[j])
j++}}return n - j
}}
classSolution{funclengthOfLongestSubstringTwoDistinct(_ s:String)->Int{let chars =Array(s)let n = chars.count
var count =[Character:Int]()var j =0for i in0..<n {
count[chars[i],default:0]+=1if count.count >2{
count[chars[j]]!-=1if count[chars[j]]==0{
count.removeValue(forKey: chars[j])}
j +=1}}return n - j
}}
Time & Space Complexity
Time complexity: O(n)
Space complexity: O(1) since we have at most 52 different characters.
Common Pitfalls
Forgetting to Remove Characters with Zero Count
When shrinking the window from the left, you must remove characters from the hash map when their count drops to zero. If you only decrement the count without removing the entry, the map size will incorrectly reflect more distinct characters than actually exist in the window, causing premature window shrinking.
Confusing "At Most Two" with "Exactly Two"
The problem asks for substrings with at most two distinct characters, not exactly two. This means substrings with zero or one distinct character are also valid. Ensure your solution counts these cases correctly and does not skip windows with fewer than two distinct characters.
Incorrect Window Boundary Updates
A common mistake is updating the result before fully adjusting the window to be valid. Always ensure the window contains at most two distinct characters before calculating and updating the maximum length. Update the result after the while loop that shrinks the window, not before.