Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Why do we need to delete the leftCharacter once its frequency reaches 0?

florian.vogt

Mar 18, 2022

Why do we need to delete the leftCharacter once its frequency reaches 0?

0

0

Comments
Comments
Design Gurus
Design Gurus4 years ago

Because we want to bring the size of charFrequencyMap equal to 'K' while shrinking the window. If we don't delete the leftChar from the map when its frequency is zero, our frequency map will not shrink.

S
Siri 4 years ago

what exactly this Map.put doing in this program? charFrequencyMap.put(rightChar, charFrequencyMap.getOrDefault(rightChar, 0) + 1);

F
florian.vogt 4 years ago

Could this also be solved using a set instead of a hash map?

T
Tiago Machado3 years ago

I have the same feeling that it can be solved with a set, but still haven't found how to to do it

V
Victor Lee3 years ago

dont think a set would work because you would be unable to track how many dupes

Design Gurus
Design Gurus3 years ago

We can't use a set, as we need to keep track of the frequency of each character.

On this page