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

0% completed

Vote For New Content
Hi everyone, is there any disadvantage to using the same conditions as the previ...

Gideon

Mar 24, 2022

Hi everyone, is there any disadvantage to using the same conditions as the previous question?

That is, in order of the highlighted code we use these conditions:

  1. if (charFrequency[rightChar] == 0)
  2. while (matched === charFreqMap .size)

This works for the provided test cases, and on LeetCode.

I think the logic checks out because all we are doing is keeping track of if each character in the pattern is matched in the current window.

2

0

Comments
Comments
E
Eduardo Sánchez4 years ago

I thought the same; but then I found the next case: 'aabaaxxxcb', 'ab'

Then the conditions you put would not satisfy this test case.

G
Gideon 4 years ago

Hi Eduardo, thank you for your response. For me, the test case that you provided works with the conditions I mentioned.

On leetcode, it's an invalid testcase because it is mentioned that the answer will be unique, whereas this testcase has multiple answers.

But if you...

J
Jharol Rivera3 years ago

I really struggled to understand why it was changed the conditions at that part, at the end both don't do the same?

S
Sukumar 3 years ago

+1 to this question, hope design gurus answer this.

O
Ozan 3 years ago

+1

Design Gurus
Design Gurus3 years ago

Can anyone share the full code with the new conditions?

D
Douglas 3 years ago

yes def f(str1, pat): start_index = 0 end_index = -1 min_size = math.inf window_start = 0 char_freq = {} matched = 0 for letter in pat: if letter not in char_freq: char_freq[letter] = 1 else: char_freq[letter] += 1

for window_end in range(len(str1)): right_char = str1[...

D
Douglas 3 years ago

mine worked in leetcode. the lines that changed are: if char_frequency[right_char] >= 0: if char_frequency[right_char] == 0:

and while matched == len(pattern): while matched == len(char_frequency):

if we compare with the size of pattern we count every letter as a matc...

On this page