educative.io

>=, ==, Problem Challenge 3 of Sliding Window

In line 30 of the solution,

if char_frequency[left_char] == 0:
matched -= 1

why do we only deduce the matched number when ==, but not >=

in line 17,

if char_frequency[right_char] >= 0:

it is >=

I changed the == to >=, the results are the same


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Solution Review: Problem Challenge 3 - Grokking the Coding Interview: Patterns for Coding Questions

Hi @Lingjie_Chen

Both >= and == will work as whenever the condition gets the value equal to 0, it will stop and shrink the window. In the first case, charFrequency[rightChar] is considering the frequency of the right side of the window therefore greater than 0 must be included but in the second case charFrequency[leftChar] is considering the frequency of the left side of the window where only redundant matching characters are left, therefore when an exact matched count appears it’ll be decremented.

Hm I still don’t really get it. Would you mind elaborating even further?

In line 17, why is char_frequency[right_char] >= 0

and not char_frequency[right_char] === 0:


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Solution Review: Problem Challenge 3 - Grokking the Coding Interview: Patterns for Coding Questions

Because the while loop condition is:
while matched == len(pattern): # matching the number of characters in the pattern
instead of
while matched == len(char_frequency): # matching the number of keys in the pattern