Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Sort Characters By Frequency
Bookmark
Input
Example 1
Example 2
Example 3
Custom
s
=
tree
INPUT
t
r
e
e
sort the characters by descending frequency
INPUT
t
r
e
e
sort the characters by descending frequency
INPUT
t
r
e
e
curr
FREQUENCY MAP
t
:
1
count '
t
' →
1
INPUT
t
r
e
e
curr
FREQUENCY MAP
t
:
1
r
:
1
e
:
1
count '
e
' →
1
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
heap empty
build a
max-heap
keyed on frequency
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
e
2
r
1
push '
r
' with frequency
1
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
e
2
r
1
t
1
push '
t
' with frequency
1
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
r
1
t
1
max
e
2
pop max → '
e
' ×
2
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
t
1
max
r
1
RESULT
e
e
pop max → '
r
' ×
1
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
MAX-HEAP
heap empty
max
t
1
RESULT
e
e
r
pop max → '
t
' ×
1
INPUT
t
r
e
e
FREQUENCY MAP
t
:
1
r
:
1
e
:
2
RESULT
e
e
r
t
sorted = "eert"
algo
master
.
io
Step:
Sort characters by frequency using a max-heap
0 / 17
Input
Example 1
Example 2
Example 3
Custom
s
=
tree
0 / 17
algo
master
.
io
Step:
Sort characters by frequency using a max-heap