Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Longest Substring with At Most K Distinct Characters
Bookmark
Brute Force
Hash Map
Ordered Map
Input
Input 1
Input 2
Input 3
k = 3
Custom
s
=
eceba
,
k
=
2
0
1
2
3
4
e
c
e
b
a
lastIndex
{ }
0
1
2
3
4
e
c
e
b
a
lastIndex
{ }
0
1
2
3
4
maxLength = 1
e
c
e
b
a
left
right
include s[
1
] =
'c'
distinct
1
/
2
charCount
'e': 1
0
1
2
3
4
maxLength = 2
e
c
e
b
a
left
right
charCount[
'e'
] =
2
distinct
2
/
2
charCount
'e': 2
'c': 1
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
include s[
1
] =
'c'
distinct
0
/
2
charCount
{ }
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
distinct =
2
≤ k = 2
valid
distinct
2
/
2
charCount
'c': 1
'e': 1
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
include s[
2
] =
'e'
distinct
0
/
2
charCount
{ }
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
distinct =
2
≤ k = 2
valid
distinct
2
/
2
charCount
'e': 1
'b': 1
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
charCount[
'b'
] =
1
distinct
1
/
2
charCount
'b': 1
0
1
2
3
4
maxLength = 3
e
c
e
b
a
left
right
distinct =
2
≤ k = 2
valid
distinct
2
/
2
charCount
'b': 1
'a': 1
0
1
2
3
4
maxLength = 3 "ece"
e
c
e
b
a
maxLength =
3
charCount
'a': 1
algo
master
.
io
Step:
Start of algorithm
0 / 51
Input
Input 1
Input 2
Input 3
k = 3
Custom
s
=
eceba
,
k
=
2
0 / 51
algo
master
.
io
Step:
Start of algorithm