Count of Substrings Containing Every Vowel and K Consonants II - Leetcode 3306 - Python

preview_player
Показать описание


0:00 - Understand the problem
5:24 - Drawing Explanation
8:35 - Coding Explanation

leetcode 3306

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

i got so many tle and wa around 600-700 tests it's unbelievable, gave up and came here

myyfstz
Автор

Before NeetCode Publishing : Acceptance Rate 26.2%
After NeetCode Publishing : Acceptance Rate 35.9%

cookit_dev
Автор

atleast k = k size + k+1 size + k+2 size +
= k size + atleast k+1
k size = atleast k+1 - atleast k

Simran_
Автор

You have given the best solution I've seen so far and explanations are very well and crisp.

aravinds
Автор

I am a bit confused about the res += (len(word) - r) and also the fact of func(k) - func(k+1). Not sure how to identify when to do this and not the regular sliding window. Any tips anyone?

business_central
Автор

k=1;

k; input: aeiqouaq = 5 = [ [aeiqou]aq, [aeiqoua]q, [aeiqouaq], a[eiqoua]q, a[eiqouaq] ] 5 valid windows
k+1: input: aeiqouaq = 2 [ [aeiqouaq], a[eiqouaq] ] 2 valid windows <-- here we need k + 1 consonants which is equal to 2 in our case two 's' for it to be counted as valid

5 - 2 = 3 substring that match all vowels with exactly k consonants

ivandrofly
Автор

hmm its interesting to think about what is and isn't considered "sliding window"

even without relaxing the second requirement (turning "exactly k consonants" into "at least k consonants"), we can still reach some semblance of a sliding window approach (approach 1 in the official solution).

the only difference is that we have to go through the trouble of temporarily expanding a valid window to see how many extra valid windows we can find before we encounter a consonant that will break our "exactly k consonant" rule. we can see that this can be optimized to an O(1) operation after doing an initial precomputation O(n) scan of the input array.

i'm unsure how pedantic one should be with these different LC techniques, but even for this approach, there is still the idea of adjusting your window wisely and responding to what requirements are being violated/satisfied. Maybe the most important characteristic is that you're avoiding repeated examinations of substrings (like a brute force approach would).

i'm the original author of the solution article btw which is why this video in particular caught my eye 😅, great video!

james-wo
Автор

This solution was way better than the leetcode editorial as usual

trueinviso
Автор

The pattern is, when there’s exact k consonants in our window. Shifting either left pointer or right pointer will lose some valid solutions. Therefore, to cope this, we have to take one side, and subtract the extra by minus f(k + 1).

travelermk
Автор

I got stuck, so I looked at the hints. It said to use binary search, and so I got even more stuck. Once I knew that I can solve for the atLeast version everything became so clear!

huangeric
Автор

i watched it 4th time, then i understood. but i don't quite until i learned. thank you NeetCode from Bangladesh

gourgupaltalukder
Автор

Really helpful video. I was happy that after learning the trick I could code out an almost identical solution to the one you ended up showing

felixx
Автор

Man that's a clever solution! Loved it!

nandanbhowmick
Автор

This is from a contest and I didn't make it during that contest. Now I learned, thank you.

furynick
Автор

the editorial's space optimization is pretty clean

pastori
Автор

Thank you so much for the clear explanation, this one really had me stumped.

jamestwosheep
Автор

In your old video you did maximum k and maximum k - 1 now you are doing atleast k and atleast k + 1 why? I couldn't figure it out myself bcz I'm in stress and stuck with this problem for 3 hrs. i.e why you went atMost to atLeast?

sheik-tlvu
Автор

Splendid solution. That was a very good trick.

aadil
Автор

bro is famous enough to be on thumbnails now

CodeWithSloth
Автор

could a 3 pointers approach work there?

sdemji
join shbcf.ru