class Solution:
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
from collections import defaultdict
left = 0
max_len = 0
char_count = defaultdict(int)
for right, char in enumerate(s):
char_count[char] += 1
while len(char_count) > 2:
char_count[s[left]] -= 1
if char_count[s[left]] == 0:
del char_count[s[left]]
left += 1
max_len = max(max_len, right - left + 1)
return max_len
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
unordered_map<char, int> count;
int left = 0, maxLen = 0;
for (int right = 0; right < s.size(); ++right) {
count[s[right]]++;
while (count.size() > 2) {
count[s[left]]--;
if (count[s[left]] == 0) {
count.erase(s[left]);
}
left++;
}
maxLen = max(maxLen, right - left + 1);
}
return maxLen;
}
};
import java.util.*;
class Solution {
public int lengthOfLongestSubstringTwoDistinct(String s) {
Map<Character, Integer> map = new HashMap<>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
map.put(c, map.getOrDefault(c, 0) + 1);
while (map.size() > 2) {
char leftChar = s.charAt(left);
map.put(leftChar, map.get(leftChar) - 1);
if (map.get(leftChar) == 0) {
map.remove(leftChar);
}
left++;
}
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
}
var lengthOfLongestSubstringTwoDistinct = function(s) {
let left = 0, maxLen = 0;
const count = new Map();
for (let right = 0; right < s.length; right++) {
count.set(s[right], (count.get(s[right]) || 0) + 1);
while (count.size > 2) {
count.set(s[left], count.get(s[left]) - 1);
if (count.get(s[left]) === 0) {
count.delete(s[left]);
}
left++;
}
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
};
Given a string s
, find the length of the longest substring that contains at most two distinct characters. You must return a single integer representing the maximum length of such a substring.
s
(a string)
Let's break the problem down. We're looking for the longest continuous segment in s
that has no more than two different types of characters.
A brute-force approach would be to check every possible substring and count the number of unique characters in each. However, this would be very slow because there are O(n2) substrings and counting unique characters for each would be expensive.
Instead, we can use a "sliding window" approach. This means we keep track of a window (a range of indices) in the string that always contains at most two distinct characters. As we scan the string from left to right, we expand the window to include new characters, and shrink it from the left when we have more than two distinct characters.
The key insight here is that by using a hash map (dictionary) to count characters in our window, we can efficiently know when we have more than two distinct characters, and adjust the window accordingly.
Let's walk through the optimized solution step by step:
left
and right
, both at 0. These define the current window.max_len
to record the maximum window size found.right
forward one character at a time, adding the character at right
to the hash map and updating its count.left
forward, reducing the count of the character at left
in the hash map.left
until the window is valid again (at most two distinct characters).max_len
if the current window is larger than any seen before.max_len
as the answer.We use a hash map because it allows us to quickly check how many distinct characters are in our window (just check the size of the map), and to efficiently increment or decrement counts as we move the window.
Let's use the input s = "eceba"
.
left = 0
, right = 0
, window = "", map = {}.
max_len = 1
.
max_len = 2
.
max_len = 3
.
max_len
remains 3.
max_len
remains 3.
By shifting from a brute-force strategy to a sliding window with a hash map, we efficiently find the longest substring with at most two distinct characters in linear time. The key is to manage the window so that it always contains at most two types of characters, expanding and shrinking as needed. This approach is both simple and powerful, making it a great example of using data structures to optimize string problems.