forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_substring_two_distinct.py
48 lines (38 loc) · 1.26 KB
/
longest_substring_two_distinct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def length_of_longest_substring_two_distinct(s):
"""
Finds the length of the longest substring with at most two distinct characters.[https://www.geeksforgeeks.org/window-sliding-technique/]
Args:
s (str): The input string.
Returns:
int: The length of the longest substring with at most two distinct characters.
Examples:
>>> length_of_longest_substring_two_distinct("eceba")
3
>>> length_of_longest_substring_two_distinct("ccaabbb")
5
>>> length_of_longest_substring_two_distinct("abcabcabc")
2
>>> length_of_longest_substring_two_distinct("")
0
"""
n = len(s)
if n == 0:
return 0
# Dictionary to store the last occurrence of each character
char_map = {}
left = 0
max_length = 0
# Sliding window approach
for right in range(n):
char_map[s[right]] = right
# If we have more than two distinct characters
if len(char_map) > 2:
# Remove the leftmost character
del_idx = min(char_map.values())
del char_map[s[del_idx]]
left = del_idx + 1
max_length = max(max_length, right - left + 1)
return max_length
if __name__ == "__main__":
import doctest
doctest.testmod()