Longest Repeating Character Replacement - Leetcode 424 - Python

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


0:00 - Read the problem
2:28 - Drawing Explanation
16:48 - Coding Explanation

leetcode 424

#character #replacement #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.
Рекомендации по теме
Комментарии
Автор

I love that you always start with the most obvious brute-force solution first, and then show how to optimize it after. It makes these problems so much easier to digest.

elheffe
Автор

For those who are struggling to understand the optimisation with maxf, here is how i understood it:

For a substring to be valid, we need window_length - maxf <= k. Here, maxf is the frequency of the most common character in the current window. The difference window_length - maxf tells us how many characters we'd need to change to make the whole window the same character.

The biggest valid substring we can get is of size maxf + k. So, the larger maxf is, the better. If maxf doesn't change or goes down, our potential best answer doesn't change. We don't need to update maxf in this case.

On the other hand, if maxf goes up, it means we've found a character in the current window that appears more often than in previous windows. This means we might be able to get a longer valid substring, so we update maxf.

Hope this helps! And thank you neetcode for the wonderful video as always.

jeremyyeo
Автор

Please keep doing this for more Leetcode problems - nobody explains them like you do. This channel deserves so many more subscribers!

kartiksoni
Автор

Having the visualization was really helpful and now the problem seems much simpler. I like how you also explained the max frequency count optimization and the logic behind it. Thanks for your videos!

jessiz-
Автор

it's not easy to get one's head around this one, thanks for working it through!💚

ladyking
Автор

You're the best out there — thank you for everything!

kristofferpanahon
Автор

there are many channel who has created the content for this problem but you only have explained why do we no need to update maxF in while loop. No fake knowledge, you are pure talent.

joydeeprony
Автор

Very helpful video. I learned how to build up the idea from scratch. It's needed especially when you explain to the interviewer in a tech screening. Thanks a lot!

celialiang
Автор

The optimization with maxf is so freaking genius! Thanks for taking time to make this. Grateful🙏

AkshayAmbekar-kdzm
Автор

the best of the best. I'm learning a lot from you. Huge credit to you my guy!

vudat
Автор

Loved the second approach.. Optimized Channel - No complexity in finding good videos .. this defines NeetCode

anmolpansari
Автор

fantastic explanation, the first time I get very clearly why we don't need to decrement the maxF var. much love to you, some hugs and kisses as well, but in a very professional and thankful manner.

triscuit
Автор

Awesome as always !! Love to watch your videos ! Please do the system design videos as you're very good in explaining concepts :)

RanjuRao
Автор

Thanks a lot for this solution, I came up with a bit complex recursive + DP solution with O(n*n) complexity and was proud of myself unless I saw your solution.
Also keep up the great work you're doing!

madhavoberoi
Автор

I love the way you explain and even teaching DSA with python. Loved it TY

shriyanshkhandelwal
Автор

Thank you so much for this video! Loved the part from 12:10 onwards.
Just adding on, I realised one possible small tweak is that we can use an if condition, instead of a while condition. Saw the following in one of the LC's comments and felt it is really enlightening: "We have a "longest" window already so finding another one of the same size is not helpful. Yeah, we might shrink the window, then extend it, then find another window of the same size as longest but in the end we will see no improvement as long as it it's just as long as longest."

jinyang
Автор

If someone is wondering like me - "Why is while loop and if statement, both giving correct result?"
The answer is: Suppose we use while loop. It will enter the while loop, only when (strLength - maxFreq) is just greater than k by " 1 ". And as soon as this happens, we decrease our String size by 1, by shifting the left pointer. So, now strLength - maxFreq = k. So, while condition will break.
In short- While condition will be satisfied only once. So, why not use an "if" instead. Hope this helps someone.
Thanks @NeetCode. You've done a great job.

SandeepKumar
Автор

This is one of those problems that literally makes no sense to me, like why would we ever need to do this? Tech hiring is so weird...

whitest_kyle
Автор

You're doing an awesome job!! Thank you! Keep up the work, you're helping a lot of us!

anishr
Автор

The max(count.values()) amazed me, I had written an extra function to look for that. You're the GOAT of this man, thanks a lot for your hard work ♥

saravalls
visit shbcf.ru