Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
String Compression
Bookmark
Brute Force
Two Pointers
Input
Example 1
Single char
Large count
Custom
chars
=
[a, a, b, b, c, c, c]
new length =
0
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
new length =
0
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
new length =
0
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
i
chars[0] =
'a'
starts a run
count =
2
new length =
0
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
i
count =
2
consecutive 'a'
count =
2
new length =
2
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
i
count =
2
> 1 → result.append(
"2"
)
count =
2
new length =
2
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
i
count =
2
consecutive 'b'
count =
2
new length =
3
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
b
i
result.append(
'b'
) len =
3
new length =
4
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
b
2
i
chars[4] =
'c'
starts a run
count =
3
new length =
5
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
b
2
c
i
result.append(
'c'
) len =
5
count =
3
new length =
6
0
1
2
3
4
5
6
a
a
b
b
c
c
c
result
a
2
b
2
c
3
i
count =
3
> 1 → result.append(
"3"
)
new length =
6
0
1
2
3
4
5
6
a
2
b
2
c
3
c
result
a
2
b
2
c
3
i
return 6
-> "a2b2c3"
algo
master
.
io
Step:
Compress [a, a, b, b, c, c, c] with an extra result array
0 / 15
Input
Example 1
Single char
Large count
Custom
chars
=
[a, a, b, b, c, c, c]
0 / 15
algo
master
.
io
Step:
Compress [a, a, b, b, c, c, c] with an extra result array