String - 15: Max sub-string length with same letters if max 'K' characters can be replaced | Java

preview_player
Показать описание
Solution:
- We iterate each character one by one & add into map & increase the frequency by one
- We also maintain maxFrequency for any letter
- We update maxLength as end - start + 1
- Now at every iteration, we check if end - start + 1 - maxFrequency is greater than k, then we need to remove it from map so we decrease the frequency by 1
- At last, we return maxLength

Time Complexity: O(n), where n is number of characters in String
Space Complexity: O(1)

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 500+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Very nicely explained. I watched many videos but no one could explain it well... You did a great job👍

KaranjitKaur
Автор

Thank you for this. My problem was why we don't reduce maxFreq, it is that if we already have a length from the maxFreq we cannot do better length until our freq increases. So if we ignore if it reduces it will not affect our final answer. Thanks again.

Megabushbuck
Автор

Greatest explanation ever of this problem. ✌️

biswajitsingh
Автор

You made this tricky problem so easier man. Thanks!

shradheytripathi
Автор

Very nicely explained, Sir, thank you so much :)

gokulnaathb
Автор

We need to remove " k<=0 "in base case because s="AAAA" and k=0 input its give -1 output but actual answer is 4.

jaykava
Автор

what if the if condition: if(end-start+1 - maxFrequency > k) is till not valid after you move the start++? I so i think you might need another for loop to keep move the start to it's right until that if condition matches?

po-shengwang
Автор

Thank you man.Could you make videos on Sets Data Structure please? Love from Bangladesh

Selim_Hasan_Raj
Автор

can u make a video on how the *.conains()* method works in a hash set in O(1) time??

mohammedsadiq
welcome to shbcf.ru