0% found this document useful (0 votes)
43 views2 pages

Given A String, Find The Longest Substring With at Most 2 Different Characters

The document describes a method to find the longest substring with at most 2 different characters in a given string. It uses a sliding window approach, maintaining a window represented by start and end indices and tracking character counts in a dictionary. The window expands until there are more than 2 distinct characters, then contracts from the left until only 2 remain, updating the maximum length. A Python function implementing this runs in linear time with respect to the string length.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views2 pages

Given A String, Find The Longest Substring With at Most 2 Different Characters

The document describes a method to find the longest substring with at most 2 different characters in a given string. It uses a sliding window approach, maintaining a window represented by start and end indices and tracking character counts in a dictionary. The window expands until there are more than 2 distinct characters, then contracts from the left until only 2 remain, updating the maximum length. A Python function implementing this runs in linear time with respect to the string length.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

‭ iven a string, find the longest substring with at most 2 different characters.


G
‭Example:‬
‭“abcab” -> “ab”‬
‭"cacaaabbac" -> "cacaaa"‬
‭"ccaaabbac" -> "aaabba"‬
‭"aabbccbb" -> "bbccbb"‬
‭"ccaaabbacccccccccc” -> "acccccccccc"‬

‭To find the longest substring with at most 2 different characters in a given string, you can use a‬

‭sliding window approach. Here's a Python function to accomplish this:‬

‭python‬

‭Copy code‬

def‬‭
‭ longest_substring_with_two_chars‬
(s):‬

if‬‭
‭ not‬‭
s:‬
return‬‭
‭ ""‬

max_length =‬‭
‭ 0‬
start =‬‭
‭ 0‬
char_map = {}‬

for‬‭
‭ end‬‭
in‬‭
range‬
(‭
‭l‬en‬
(s)):‬

right_char = s[end]‬

char_map[right_char] = char_map.get(right_char,‬‭
‭ 0‭
)‬‬‭
+‬‭
1‬

while‬‭
‭ len‬
(char_map) >‬‭
‭ 2‬:‬

left_char = s[start]‬

char_map[left_char] -=‬‭
‭ 1‬
if‬‭
‭ char_map[left_char] ==‬‭
0‭
:‬‬
del‬‭
‭ char_map[left_char]‬
start +=‬‭
‭ 1‬

max_length =‬‭
‭ max‬
(max_length, end - start +‬‭
‭ 1‭
)‬‬

return‬‭
‭ s[start:start + max_length]‬

# Example usage:‬

print‬
‭ (longest_substring_with_two_chars(‬
‭ "abcab"‬
‭ )) #‬‭
‭ Output: "ab"‬
print‬
‭ (longest_substring_with_two_chars(‬
‭ "cacaaabbac"‬
‭ ))‬‭
‭ # Output: "cacaaa"‬
print‬
‭ (longest_substring_with_two_chars(‬
‭ "ccaaabbac"‬
‭ ))‬‭
‭ # Output: "aaabba"‬
print‬
‭ (longest_substring_with_two_chars(‬
‭ "aabbccbb"‬
‭ ))‬‭
‭ # Output: "bbccbb"‬
print‬
‭ (longest_substring_with_two_chars(‬
‭ "ccaaabbacccccccccc"‬
‭ ))‬‭
‭ # Output: "acccccccccc"‬
‭This function maintains a sliding window represented by the variables‬‭
start‬‭and‬‭ ‭. It also keeps‬
end‬

‭track of the count of characters in the current window using the‬‭


char_map‬‭dictionary. The window‬

‭expands until there are more than 2 distinct characters. When this happens, the window contracts‬

‭from the left side until there are only 2 distinct characters again, updating the‬‭
max_length‬‭along the‬

‭way. Finally, it returns the longest substring found.‬

You might also like