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

0% completed

Vote For New Content
"If at any time, the number of characters matched is equal to the number of dist...

dg

May 17, 2022

"If at any time, the number of characters matched is equal to the number of distinct characters in the pattern (i.e., total characters in the HashMap), we have gotten our required permutation."

Why is this true? What if I have, lets say pattern 'abb' and string 'bacdfghi'. The distinct keys a and b are in bacdfghi', but the counts of b don't match. I see that the code works/returns the proper result (false), but why, if we are only comparing keys to match count, rather than total counts?

0

0

Comments
Comments
A
aj 3 years ago

matched = how many characters were completely found i.e. found with matching count. charFrequencyMap.size() = how many unique characters are present in the given pattern.

So, when they become equal we have a match. Key thing here is that we don't remove the character f...

On this page