Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Longest Substring with At Most Two Distinct Characters
Bookmark
Brute Force
Hash Map
Last Occurrence
Input
Input 1
Input 2
All Distinct
Custom
s
=
eceba
0
1
2
3
4
e
c
e
b
a
count
{ }
0
1
2
3
4
e
c
e
b
a
count
{ }
0
1
2
3
4
max_len = 1
e
c
e
b
a
left
right
1
>
0
new best
maxLen =
1
count
'e': 1
0
1
2
3
4
max_len = 2
e
c
e
b
a
left
right
include s[
2
] =
'e'
, count =
2
count
'e': 2
'c': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
include s[
1
] =
'c'
, count =
1
count
'c': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
distinct =
2
≤ 2
valid
count
'c': 1
'e': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
include s[
2
] =
'e'
, count =
1
count
'e': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
distinct =
2
≤ 2
valid
count
'e': 1
'b': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
distinct =
1
≤ 2
valid
count
'b': 1
0
1
2
3
4
max_len = 3
e
c
e
b
a
left
right
2
≤
3
keep best
count
'b': 1
'a': 1
0
1
2
3
4
max_len = 3 "ece"
e
c
e
b
a
maxLen =
3
count
'a': 1
algo
master
.
io
Step:
Start of algorithm
0 / 38
Input
Input 1
Input 2
All Distinct
Custom
s
=
eceba
0 / 38
algo
master
.
io
Step:
Start of algorithm