L6. Longest Substring With At Most K Distinct Characters | 2 Pointers and Sliding Window Playlist

  Рет қаралды 62,336

take U forward

take U forward

Күн бұрын

Пікірлер: 111
@sindhunair3993
@sindhunair3993 2 ай бұрын
Solved before watching video, the way you have planned the playlist is simply brilliant! Thank you Striver!!
@rudrajitgupta3688
@rudrajitgupta3688 8 ай бұрын
Understood Striver. Was able to reuse the same code that was implemented in previous lecture before starting to watch the solution. Thank you again for such a great explanation
@txbankush4601
@txbankush4601 5 ай бұрын
can u provide the full code. I am getting some of my testcases failed.
@adarshshankarrai9912
@adarshshankarrai9912 5 ай бұрын
@@txbankush4601 int n,k; cin >> n >> k; string s; cin >> s; int l = 0, r = 0, maxlen = 0; mapmp; while(r < n) { mp[s[r]]++; if(mp.size() > k){ while(mp.size() > k){ mp[s[l]]--; if(mp[s[l]] == 0){ mp.erase(s[l]); } l++; } } if(mp.size()
@SHARNAMKANSAL
@SHARNAMKANSAL 5 ай бұрын
@@txbankush4601 int l=0,r=0,maxi=-1; unordered_mapmpp; while(rk) { mpp[s[l]]--; if(mpp[s[l]]==0)mpp.erase(s[l]); l++; } if(mpp.size()==k) { maxi=max(maxi,r-l+1); } r++; } return maxi;
@shashankshekhar1713
@shashankshekhar1713 5 ай бұрын
@@txbankush4601 for previous problem int totalFruits(int N, vector &arr) { int left = 0, right = 0, maxLength = 0; unordered_map map; while (right < N) { map[arr[right]]++; if (map.size() > 2) { map[arr[left]]--; if (map[arr[left]] == 0) { map.erase(arr[left]); } left++; } if(map.size()
@harshpratapsingh2075
@harshpratapsingh2075 4 ай бұрын
@@txbankush4601 class Solution{ public: int longestKSubstr(string s, int k) { unordered_map m; for(auto it: s) m[it]++; if(m.size()
@sunitsable2752
@sunitsable2752 3 ай бұрын
Solved without watching video !!! Thank u striver ❤
@Sankalp-sd6fm
@Sankalp-sd6fm 2 ай бұрын
How do you guys solving problems which need subscription to unlock.
@keerthishankar2341
@keerthishankar2341 Күн бұрын
@@Sankalp-sd6fm search the problem in geeks for geeks and solve!
@anishaa3298
@anishaa3298 9 күн бұрын
i was able to solve this by my own after watching first 2 mins of your video and previous lectures thank youuuuu!!!!! God bless
@AKASHKUMAR-li7li
@AKASHKUMAR-li7li 4 ай бұрын
After struggling... i solved this question by myself confidence++, Thank you striver
@arnavshukla4271
@arnavshukla4271 4 ай бұрын
Thank you Striver! Was able to solve this question based on the understanding of the previous lecture
@FutureForge-ir7xc
@FutureForge-ir7xc 7 ай бұрын
Thank you so much Striver!!!!
@AkshitChaudhary-vx8iw
@AkshitChaudhary-vx8iw 7 ай бұрын
thankyou so much Striver. i solved this question at my own just because of your previous lectures. you are amazing😍
@Sankalp-sd6fm
@Sankalp-sd6fm 2 ай бұрын
How do you guys solving problems which need subscription to unlock.
@AK-nj1je
@AK-nj1je 4 ай бұрын
Actually the space complexity of the most optimal approach will be almost O(n) Coz just imagine all the elements are different then, the code will not care what's the size of the map, it will just keep adding elements in the map, howeven the maxlen will not be affected. Amazing lecture!!!!
@vinaykumarratnala5832
@vinaykumarratnala5832 3 ай бұрын
Solved before watching the video. watching now to see if i learn anything new.
@tejasjaulkar9658
@tejasjaulkar9658 7 ай бұрын
lot of love from us striver ......... thank u so muchhhhh
@harpreetnarwal6250
@harpreetnarwal6250 7 ай бұрын
Thank you so much brother!! was able to do this before watching the video all thank to you!!
@gauravlandge3356
@gauravlandge3356 7 ай бұрын
I used to struggle a lot with sliding window Questions Thanks to u and your playlist because of which i am able to solve this type of Questions. Thankyou very much.
@gourabrajak8132
@gourabrajak8132 3 ай бұрын
public static int solution(String str,int k){ int n =str.length(); int maxLength =0; for(int i=0;i
@prateekshrivastava2802
@prateekshrivastava2802 5 ай бұрын
Previous question is same to this one !!
@chrisliu657
@chrisliu657 13 күн бұрын
As an optimization to your solution, what if you tried storing the last index that each character appears in the dictionary? That way, when the # of distinct elements exceeds k, you shift i by the min value in your dictionary (which effectively skips over a certain character in the subarray). The only issue with this would be retrieving the min value. Great video btw!
@shwetanshu13
@shwetanshu13 7 ай бұрын
Thank you sir for such an important series
@TheK_Fam
@TheK_Fam 7 ай бұрын
One note here is that if the answer is not possible you would need to only update the max_len when len(ch_map) == k and initiate the max_len to be -1
@Sankalp-sd6fm
@Sankalp-sd6fm 2 ай бұрын
How do you guys solving problems which need subscription to unlock.
@tanazshaik678
@tanazshaik678 11 күн бұрын
this was available on code360
@augustinradjou3909
@augustinradjou3909 7 ай бұрын
I solved the question 😅 and then watching to get more idea ...little bit change of previous question....tryit guys then see the video❤
@14-a-akritiawasthi35
@14-a-akritiawasthi35 Ай бұрын
bhaiya apne aap kiya hmne........................thankyou bhaiya
@albert00991
@albert00991 16 күн бұрын
The space complexity of brute force should be O(K+1) because after that the break statement will get executed
@hashcodez757
@hashcodez757 2 ай бұрын
"UNDERSTOOD BHAIYA!!"
@LearnWithMe7777
@LearnWithMe7777 5 ай бұрын
Similar as previous in last lec Understood ❤
@Hello1234-vh5sm
@Hello1234-vh5sm 4 ай бұрын
The code only considers the number of unique characters (map.size()) within the window to determine validity. However, for the problem of finding the maximum length of a substring with at most k replacements, we need to consider the frequency of the most frequent character within the window.
@vivekrajput7400
@vivekrajput7400 4 ай бұрын
can you explain what exactly are you trying to say?
@yaminisabbavarapu2538
@yaminisabbavarapu2538 4 ай бұрын
Actually not most frequent least or 2nd most frequent
@racks8493
@racks8493 Ай бұрын
i think your queestion's solution is in lecture - 8
@tanujaSangwan
@tanujaSangwan 2 ай бұрын
Solved it without watching the video
@joshl3013
@joshl3013 27 күн бұрын
Very helpful. Liked and subcribed
@augustinradjou3909
@augustinradjou3909 7 ай бұрын
Its okay to opt with map it is much more intuitive...i feel but yeah vector is more good ofcourse😅
@KrishanKumar-lp2cd
@KrishanKumar-lp2cd 2 ай бұрын
please Striver make video on heaps
@Godlike_FTW
@Godlike_FTW 8 ай бұрын
Sir i am following ur course should i start with stack and queues(acc. To other yt channel) or start linklist i had completed till binnary search ❤❤❤
@samagraagrawal5333
@samagraagrawal5333 7 ай бұрын
Link list se start Karo. Stack and queue ke implementation mein link list ka use aayega
@THOSHI-cn6hg
@THOSHI-cn6hg 3 ай бұрын
u should have taken some problem intead of this , becoz this has been previoulsly covererd
@gnanaphanideep6398
@gnanaphanideep6398 Ай бұрын
Understood bro😁
@dhruv7655
@dhruv7655 4 ай бұрын
Why not store the latest index of the character into the map instead of storing frequency? and once the condition fails, we can simply use an map iterator and find the min index stored in the map then just update L = ind + 1.
@MdSarfrazcs
@MdSarfrazcs Ай бұрын
l-5 same question where we have to care about two distinct element in this we have to think about k distinct element and nothing else
@bunnysahith4810
@bunnysahith4810 7 ай бұрын
similar to approach in l5
@hemantranjan2297
@hemantranjan2297 Ай бұрын
Will bruter force run on string: "a" and k = 0, 1, 2
@samsingh43
@samsingh43 8 ай бұрын
it's better to use Vector rather than Hashmap
@30sunique78
@30sunique78 7 ай бұрын
Why ?
@samagraagrawal5333
@samagraagrawal5333 7 ай бұрын
​@@30sunique78 Hashmap works in logarithmic time , while vector/array/list in constant time Although if you are working in c++ there is also unordered map which works in constant time but still even then it is prefered to work with vectors as the constants involved with vector are better than hash maps
@harshi993
@harshi993 6 ай бұрын
O(1) time complexity and only O(26) space
@varenyabarve69
@varenyabarve69 5 ай бұрын
We wont able to access this question, it's saying to view this question you must subscribe to premium 😢 . Now how to do that ?
@anandraj3995
@anandraj3995 5 ай бұрын
do it on coding ninja
@saatvikmangal7994
@saatvikmangal7994 5 ай бұрын
@@anandraj3995 tysm bro
@prateekshrivastava2802
@prateekshrivastava2802 5 ай бұрын
Gfg me same question h
@shashankshekhar1713
@shashankshekhar1713 5 ай бұрын
@@prateekshrivastava2802 link?
@Dharmik_Vibes
@Dharmik_Vibes 5 ай бұрын
Same as Fruit In basket
@shreyxnsh.14
@shreyxnsh.14 Ай бұрын
same as previous question
@subee128
@subee128 7 ай бұрын
Thank you very much
@prathameshlendghar827
@prathameshlendghar827 5 ай бұрын
Question in sheet is not correctly mapped with the video It expects to interchange any k char and by doing so find the longest string with same character and in the video it is changing all occurances of a distinct characters and for all instance of k different character.
@Sahilsharma-sk5vr
@Sahilsharma-sk5vr 5 ай бұрын
right
@_sf_editz1870
@_sf_editz1870 8 ай бұрын
Solved this 💕💕
@BhupeshReddy332
@BhupeshReddy332 7 ай бұрын
Why was it O(256) space for brute force? because size is k distinct right? like as soon as size becomes k+1, we break. i didn't understand why so much extra space is being taken
@lakshsinghania
@lakshsinghania 6 ай бұрын
suppose the len of string is 256 containing all different characters and k = 256 then space will be O(256) he is writing everything in worst case thats like upper bound
@jyotirmaysingh4059
@jyotirmaysingh4059 Ай бұрын
did this on my own
@PIYUSHTAILORstillalive
@PIYUSHTAILORstillalive 7 ай бұрын
Could we use use queue to store char and the index of that character? The latest one and when size increases to 3 then we drop the front and take the latest index. Update the length and r keep on going. I just don't want that while loop running to find our l.
@adityapundir5549
@adityapundir5549 3 ай бұрын
I have one confusion that while optimizing the code we try to use 'if' instead of 'while' when we try to shift left pointer. But how does it optimize i am not getting. The iteration in both the cases will be exactly same, and i guess using while loop to shift left pointer is more efficient. Does anybody else have this confusion???
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 ай бұрын
in every iteration every character visit maximum 2time so tc(2n)==0(n);
@ajithshetty1684
@ajithshetty1684 6 ай бұрын
what happens when string is "AABABBA" Actual output will be 5 but current code will give 7, here k=2 means at max 2 times A to B conversion or vice versa can be done as per leetcode
@ajithshetty1684
@ajithshetty1684 6 ай бұрын
Inputs & expected output from leetcode 1) Input: s = "ABAB", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. 2) Input: s = "AABABBA", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA". The substring "BBBB" has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too.
@ajithshetty1684
@ajithshetty1684 6 ай бұрын
But if this question is not linked to leetcode and the expectation is to find longest substring of k dinstinct characters then solution is perfect
@DeadPoolx1712
@DeadPoolx1712 Ай бұрын
UNDERSTOOD;
@oyeesharme
@oyeesharme 3 ай бұрын
thanks bhaiya
@shashankpujari9970
@shashankpujari9970 Ай бұрын
class Solution { public: int characterReplacement(string s, int k) { int n = s.length(); int l = 0, r = 0; map mp; int maxc = 0; while(r < n) { mp[s[r]]++; if(mp.size() > k) { mp[s[l]]--; if(mp[s[l]] == 0) mp.erase(s[l]); l++; } if(mp.size()
@txbankush4601
@txbankush4601 5 ай бұрын
someone pls provide the full code. I am getting some of my testcases failed.
@codeman3828
@codeman3828 7 ай бұрын
Understood. Thanks
@ManishKumar-dk8hl
@ManishKumar-dk8hl 7 ай бұрын
import java.util.HashMap; public class Solution { public static int kDistinctChars(int k, String str) { int l=0; int r=0; int maxlen=0; HashMapmpp=new HashMap(); while(r
@aniketsingh5516
@aniketsingh5516 7 ай бұрын
not working
@ibrahimalikhan3539
@ibrahimalikhan3539 7 ай бұрын
@@aniketsingh5516 Correct this condition if(mpp.size() == k )
@aditya_raj7827
@aditya_raj7827 4 ай бұрын
understood 😊😊
@shreyaagarwal4320
@shreyaagarwal4320 7 ай бұрын
why is the TC roughly O(2n) ? it is while inside while right, so it should be O(n^2)
@youcanyouwill2004
@youcanyouwill2004 7 ай бұрын
n^2 happens only when for every constant j -> i runs till end, but here overall i runs only once and j also runs only once throughout the program. watch previous lecture striver has explained it
@deepjoysarma7495
@deepjoysarma7495 4 ай бұрын
can i have the question link please?
@AJK-a2j00k4
@AJK-a2j00k4 7 ай бұрын
Hey, I didn't understand one thing, that he mentions extra TC O(log 256) where is this coming from? plus he is saying Space complexity as O(256) but there are only 26 alphabets right, then how is it 256 instead of 26? can someone plzz clarify this with detailed explanation or some video link/ references to understand this! Thank you
@abhay3545
@abhay3545 7 ай бұрын
There are 256 characters in any programming language, 26 (A-Z)+26(A-Z)+ special characters. The problem string can have any of the characters of 256.
@AJK-a2j00k4
@AJK-a2j00k4 7 ай бұрын
@@abhay3545 ohh thnx
@adityapandey23
@adityapandey23 3 ай бұрын
Understood
@rlm3227
@rlm3227 5 ай бұрын
understood
@nikhilkumarpathak9588
@nikhilkumarpathak9588 2 ай бұрын
Notes are not available yet
@giridharanpasupathi
@giridharanpasupathi 7 ай бұрын
🙄🙄🙄🙄 Am i the only one know that the L5 and L6 are same question
@spotshot7023
@spotshot7023 7 ай бұрын
Yes, it's same
@chad._life
@chad._life 4 ай бұрын
why this code is not running anyone can tell me and correct it class Solution { public: int characterReplacement(string s, int k) { int r=0,l=0,maxlen=0,n=s.length(); unordered_mapmp; while(rk){ while(mp.size()>k){ mp[s[l]] = mp[s[l]]-1; if(mp[s[l]]==0){ mp.erase(mp[s[l]]); } l++; } } maxlen = max(maxlen,r-l+1); r++; } return maxlen; } };
@nikhilbasir
@nikhilbasir 3 ай бұрын
U erased mp[s[l]] u have to erase the value not tha frequency so mp.erase[s[l]] will be right
@victorymindset-1
@victorymindset-1 2 ай бұрын
Can someone explain me how that "while" loop into a single "if" actually works and valid? i didn't understand that
@umangagarwal8726
@umangagarwal8726 2 ай бұрын
just watch lec 1 of this playlist if u have still doubt then do tell me
@dayashankarlakhotia4943
@dayashankarlakhotia4943 2 ай бұрын
public int longestKSubstring(String s,int k){ int unique=0,len=-1,l=0; int[]freq=new int[26]; for(int i=0;ik){ ch=s.charAt(l)-'a'; freq[ch]--; if(freq[ch]==0) unique--; l++; } if(unique==k) len=Math.max(len,i-l+1); } return len; }🎉❤
@angeldeveloper
@angeldeveloper 8 ай бұрын
🎉❤
@AkashKumarTiwary-u4b
@AkashKumarTiwary-u4b 6 ай бұрын
god
@ashishpradhan6250
@ashishpradhan6250 4 ай бұрын
arigato
@p1xel3434
@p1xel3434 4 ай бұрын
public static int longestSubstringWithAtMostKDistinctCharacter(String str, int k) { int maxLength = 0; char[] chars = str.toCharArray(); int[] map = new int[26]; int uniqueChars = 0; int left = 0; int n = chars.length; for (int right = 0; right < n; right++) { if (map[chars[right] - 'a'] == 0) uniqueChars++; map[chars[right] - 'a']++; while (uniqueChars > k) { map[chars[left] - 'a']--; if (map[chars[left] - 'a'] == 0) uniqueChars--; left++; } maxLength = Math.max(maxLength, right - left + 1); } return maxLength; }
@RaghavN-rd5zw
@RaghavN-rd5zw 6 ай бұрын
Thank you so much Striver!!!
@SitaRam-m1i
@SitaRam-m1i Ай бұрын
Understood
@shashankvashishtha4454
@shashankvashishtha4454 4 ай бұрын
understood
@tanazshaik678
@tanazshaik678 11 күн бұрын
Understood
@anshsaxena7297
@anshsaxena7297 8 күн бұрын
Understood
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 154 МЛН
ТЫ В ДЕТСТВЕ КОГДА ВЫПАЛ ЗУБ😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 4,5 МЛН
CAT 2024 Review and Student Reactions
3:37
shiksha.com
Рет қаралды 20
Coding Unbreakable Encryption in C | One-Time Pad
17:42
HirschDaniel
Рет қаралды 4,4 М.
L5. Fruit Into Baskets | 2 Pointers and Sliding Window Playlist
30:02
take U forward
Рет қаралды 82 М.
L12. Minimum Window Substring | 2 Pointers and Sliding Window Playlist
27:06
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН