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

0% completed

Vote For New Content
Shouldn't the condition for shrinking the window be windowLength > patternLength...

Alex

Mar 2, 2022

Shouldn't the condition for shrinking the window be windowLength > patternLength?

In addition, I believe the if statement checking for matched == charFrequencyMap.size() should be after the code for shrinking the window. I believe it fails on the following input: str = "sraactelt" pattern = "scarlett"

0

0

Comments
Comments
E
Eric C4 years ago

That example returns false because "sraactelt" is not a permutation of "scarlett". The string contains 2 'a' which causes the permutation to not hold true;

E
Eduardo Sánchez4 years ago

I initially thought the same. Nevertheless, I learn this:

  1. windowLength > patternLength needs to be ">=" and not only ">". Using the test case ('odicf', 'dc'): When we move our window from [0,2] to [1,3] we are already running the first procedure that evaluates if we...
D
dg 3 years ago

"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 'bacdfg...

On this page