Minimum Window Subsequence | Leetcode 727

Minimum Window Subsequence
min sliding window
You can find the source code on my github account:
github.com/shehabic/java-algo...

Пікірлер: 83

  • @user-oz2eu7rs8v
    @user-oz2eu7rs8v3 ай бұрын

    In the beginning it looked like very difficult to me but after some I started getting it and at the end everything is clear. Thanks a lot bro 🔥🔥🔥🔥

  • @hqdevelopers3697
    @hqdevelopers36974 жыл бұрын

    We hope you will post more videos like this. Very valuable problems and solutions!

  • @codingatascale4927

    @codingatascale4927

    4 жыл бұрын

    Thanks for your feedback, I will have to get permission from my current employer first, I recently joined one of the FAANG companies and they have a specific policy around that.

  • @qingdong3264
    @qingdong32645 жыл бұрын

    Brilliant! Easy and clear!

  • @gabrieldjebbar7098
    @gabrieldjebbar70983 жыл бұрын

    Wow thanks for the video !! I kind of like coming up with name for techniques, I think "rebounding sliding window" would fit well for this one :D

  • @AB-dn1cq
    @AB-dn1cq4 жыл бұрын

    thanks for covering all possibilities

  • @ahmedrizk369
    @ahmedrizk3693 жыл бұрын

    Great solution, really we appreciate what you do

  • @umber3117
    @umber31174 жыл бұрын

    Thanks for sharing. Great explanation!!!

  • @runggp
    @runggp5 жыл бұрын

    awesome ! easy to follow!

  • @bhaveshsingh7230
    @bhaveshsingh7230 Жыл бұрын

    what if bring the i pointer directly to end -1instead of i++ (just calculate the the end-i

  • @LuvHeartZ
    @LuvHeartZ5 жыл бұрын

    Thanks alot , very easy explained

  • @khassangaligapparov2202
    @khassangaligapparov2202 Жыл бұрын

    At first look I thought it will be tricky one. But everything was streght and forward. Thank you for your explaination. Don't loose your motivation. Best of luck! The time complexity will be O(NxM) (N is S length and M is T length)

  • @JuswanthTeeb
    @JuswanthTeeb Жыл бұрын

    Beautiful approach !!

  • @cloud5887
    @cloud58874 жыл бұрын

    Perfect explanation.

  • @rahoolification
    @rahoolification3 жыл бұрын

    Excellent Solution !

  • @mohanreddy4669
    @mohanreddy46694 жыл бұрын

    Btw, can you please do a video on Minimum window substring if its possible?

  • @megamind9175
    @megamind91753 жыл бұрын

    I was so confused, crying my eyes out at my own stupidity, and then I found this video and I felt stupid no more, thank you habibi

  • @nosouponhead

    @nosouponhead

    3 жыл бұрын

    Actually crying?

  • @megamind9175

    @megamind9175

    3 жыл бұрын

    @@nosouponhead leetcode make me cry, it is true

  • @himzp

    @himzp

    3 жыл бұрын

    solution here is so simple, that it made me cry more :D

  • @minakshikudalkar557
    @minakshikudalkar5573 жыл бұрын

    This is a nice intuitive solution, I was wondering if there is a way to do this in O(S+T)?

  • @codingatascale4927

    @codingatascale4927

    3 жыл бұрын

    I don’t think so

  • @saicharan8675
    @saicharan86754 жыл бұрын

    Damnnn you made it simple thanks

  • @manikandansachidanandan9439
    @manikandansachidanandan94394 жыл бұрын

    This is awesome

  • @payalarya1845
    @payalarya18455 жыл бұрын

    Very nice explained

  • @swapnil814
    @swapnil8144 жыл бұрын

    Super Awesome trick.

  • @abhishek__anand__
    @abhishek__anand__ Жыл бұрын

    Great Approach

  • @alexhong794
    @alexhong7944 жыл бұрын

    very good explanation.

  • @wotmasterwot7477
    @wotmasterwot74773 жыл бұрын

    OMG this is sssooo smart !!!

  • @xysmile4722
    @xysmile47224 жыл бұрын

    Aamazing

  • @jeremyhuang1394
    @jeremyhuang13944 жыл бұрын

    Hello, I try to write a python code for your solution. But It has bug. Coud you help me to find it? Let's me know thank you

  • @aunnfriends

    @aunnfriends

    3 жыл бұрын

    class Solution: """ @param S: a string @param T: a string @return: the minimum substring of S """ def minWindow(self, S, T): window = "" i, j , min_l = 0, 0, len(S) + 1 while i if S[i] == T[j]: j += 1 if j == len(T): end = i + 1 j -= 1 while j >= 0: if S[i] == T[j]: j -= 1 i -= 1 j += 1 i += 1 if end - i min_l = end - i window = S[i:end] i += 1 return window

  • @rish2025

    @rish2025

    3 жыл бұрын

    Don't try to write a For loop in python and change the value of the iterator. The iterator will move back to its original value in the next iteration of the loop and you will not be able to traverse back.

  • @varunjain7360
    @varunjain73603 жыл бұрын

    awesome explanation! What are the complexities and why?

  • @monkeyWatts
    @monkeyWatts3 жыл бұрын

    it'd be nice to have an explanation on the time complexity

  • @vaibhavpandey5615
    @vaibhavpandey56154 жыл бұрын

    thankyou sir ,you are grate..

  • @MuscleTeamOfficial
    @MuscleTeamOfficial Жыл бұрын

    thats gangsta

  • @shresth_bhakta
    @shresth_bhakta Жыл бұрын

    amazing explanation

  • @pratvick
    @pratvick3 жыл бұрын

    Can you please tell us the time complexity of this solution?

  • @MohamedShehab

    @MohamedShehab

    3 жыл бұрын

    Worst case O(ST) , best case O(S+T) So O(ST)

  • @MohamedShehab

    @MohamedShehab

    3 жыл бұрын

    Worst case when you have a string like this: “aaaaaaaaaaaaaaaaa” and you’re looking for “aaa” for example

  • @mohanreddy4669
    @mohanreddy46694 жыл бұрын

    I am assuming it is O(2ST) which comes down to O(ST). Please correct me if I am wrong. Thanks!

  • @deepakanuraagsathyanarayan9666

    @deepakanuraagsathyanarayan9666

    4 жыл бұрын

    it should be O(S+T) right - one forward pass of S one backward pass of T

  • @codingatascale4927

    @codingatascale4927

    4 жыл бұрын

    Worst case yes O(S*T)

  • @hqdevelopers3697

    @hqdevelopers3697

    4 жыл бұрын

    @@deepakanuraagsathyanarayan9666 I agree with you: O(S+T)

  • @ljkb23

    @ljkb23

    4 жыл бұрын

    It is O(ST) because it is O(T+(# of pattern found)*S) which in worse case O(ST).

  • @user-oz2eu7rs8v
    @user-oz2eu7rs8v3 ай бұрын

    slight modification do not store substrings again and again store starting index and length and find substring at the end

  • @sameer1571
    @sameer15713 жыл бұрын

    Ohh my god what an explanation 😃😃😃😃😃😃😃😃

  • @stuartYoung559
    @stuartYoung559 Жыл бұрын

    why you`ve stopped making such amazing videos

  • @SHUBHAMKUMAR-yy2to
    @SHUBHAMKUMAR-yy2to3 жыл бұрын

    What is the time complexity of your approach

  • @codingatascale4927

    @codingatascale4927

    3 жыл бұрын

    O(ST) at worst

  • @mohanreddy4669
    @mohanreddy46694 жыл бұрын

    I wish you could explain point out the time complexity at the end of the explanation.

  • @AbhishekRaj-ew1zp
    @AbhishekRaj-ew1zp5 жыл бұрын

    Wouldn't the algorithm fail if S = XAYMBAZBDCEABE and T = ABE? The answer is ABE but since you stop at the first occurrence of E, you would output AZBDCE.

  • @codingatascale4927

    @codingatascale4927

    5 жыл бұрын

    Hi Abischek, It'll actually output ABE. If you check the algorithm you'll find that it doesn't stop with the first match, but rather continues to find more. Here's a gist if you'd like to try it by yourself: gist.github.com/shehabic/52ccbf4bb23795ae31b65566016c434f

  • @xiaoxiaozhang3924

    @xiaoxiaozhang3924

    5 жыл бұрын

    That is what I thought. So this solution is not right.

  • @codingatascale4927

    @codingatascale4927

    5 жыл бұрын

    @@xiaoxiaozhang3924 every time you find a solution you go back and count the length, and since you reset the pointer of T, you continue looking for the next occurrence, so indeed it doesn't stop until it finds all possible solutions and the shortest ones to be specific. Here's the sample with the example above. gist.github.com/shehabic/52ccbf4bb23795ae31b65566016c434f

  • @jayanthavasarala

    @jayanthavasarala

    4 жыл бұрын

    Hi Party

  • @AbhishekRaj-ew1zp

    @AbhishekRaj-ew1zp

    4 жыл бұрын

    @@jayanthavasarala Arey Paaaaaarty. Kaisa hai yaaro? Small world. Let's catch up sometime.

  • @jeremyhuang1394
    @jeremyhuang13944 жыл бұрын

    I found the bug. For loop in python is different in Java.

  • @rish2025

    @rish2025

    3 жыл бұрын

    Yeah. Don't try to write a For loop in python and change the value of the iterator. The iterator will move back to its original value in the next iteration of the loop and you will not be able to traverse back.

  • @iiiTzYedya
    @iiiTzYedya3 жыл бұрын

    Time : O(M * N) Space : O(1)

  • @anokhkishore
    @anokhkishore4 жыл бұрын

    Seems wrong .. try ABE with AdjApdoBsjsEABE

  • @codingatascale4927

    @codingatascale4927

    4 жыл бұрын

    You can try it in the sample code on github

  • @lifeofasoftwareengg...930
    @lifeofasoftwareengg...9302 жыл бұрын

    class Solution { public String minWindow(String s1, String s2) { int start =-1; int sIndex=0; int tIndex=0; int sLen = s1.length(); int len = sLen+1; while(sIndex if(s1.charAt(sIndex) == s2.charAt(tIndex)){ tIndex++; if(tIndex == s2.length()){//contract the window from right to left int end = sIndex + 1; tIndex--; while(tIndex >=0 ){ if(s1.charAt(sIndex) == s2.charAt(tIndex)){ tIndex--; } sIndex--; } //since j becomes negative tIndex++; sIndex++; if( end - sIndex len = end - sIndex ; start = sIndex; } } } sIndex++; } return start == -1 ? "" : s1.substring(start, start + len); } }

  • @ikliuger
    @ikliuger4 жыл бұрын

    This is O(N^2) since after getting the first candidate you increment i by one and start searching for the next candidate.

  • @codingatascale4927

    @codingatascale4927

    4 жыл бұрын

    No, it’s O(s*t) in the worst case, where m is the subsequence

  • @codingatascale4927

    @codingatascale4927

    4 жыл бұрын

    You increment j by 1 from the shortest subsequence found not from the first i you started with

  • @anirudhravichandran6892

    @anirudhravichandran6892

    4 жыл бұрын

    @@codingatascale4927 for every window you find, this is going to be O(S). the number of windows you can find is of the order O(S) as well, for example T="a" and S = "aaaa"

  • @tomwong3336

    @tomwong3336

    3 жыл бұрын

    ​@@anirudhravichandran6892 I agree with you. For example, this test case: S = XAYMBAZBDCEABE and T = ABE. Because when you get all the matching characters, you travel back to the last seen A and the length of this travel "AZBDCE" is longer than T, probably it's O(S). so, the time complexity for this algorithm is O(S*S) not O(S*T).

  • @aunnfriends
    @aunnfriends3 жыл бұрын

    my converted Python code class Solution: def minWindow(self, S, T): window = "" i, j , min_l = 0, 0, len(S) + 1 while i if S[i] == T[j]: j += 1 if j == len(T): end = i + 1 j -= 1 while j >= 0: if S[i] == T[j]: j -= 1 i -= 1 j += 1 i += 1 if end - i min_l = end - i window = S[i:end] i += 1 return window

  • @missizzyc
    @missizzyc Жыл бұрын

    This explanation doesn't work for "abaacabcc" with substr "abc"

  • @codingatascale4927

    @codingatascale4927

    Жыл бұрын

    Works, the code is linked feel free though try it.

  • @darshansimha2166
    @darshansimha21662 жыл бұрын

    I think minWin = S,substring(end, i) should be minWin = S,substring(i, end + 1) because end will always be greater than i.

  • @codingatascale4927

    @codingatascale4927

    2 жыл бұрын

    I linked the code, feel free to try it

  • @chillrelax5056
    @chillrelax5056 Жыл бұрын

    This solution is wrong. For S=AXBYESABE T = ABE Your code will return AXBYE. But the real answer is ABE

  • @MohamedShehab

    @MohamedShehab

    Жыл бұрын

    Code is attached, you can try it yourself

  • @chillrelax5056

    @chillrelax5056

    Жыл бұрын

    @@MohamedShehab Yeah it does. But you took the simplest possible example to do a walkthrough. I doubt you really understand the code. Or if you have just copied it from somewhere else.

  • @MohamedShehab

    @MohamedShehab

    Жыл бұрын

    @@chillrelax5056 interesting how you judge if I understand my code or not, whatever you say :), you can also find a full set of problems I solved shared on my github repo above.

  • @chillrelax5056

    @chillrelax5056

    Жыл бұрын

    @@MohamedShehab Hmm maybe you are right! I am preparing for interviews under pressure! so please ignore my comments. Peace!

  • @MohamedShehab

    @MohamedShehab

    Жыл бұрын

    @@chillrelax5056 no worries, best of luck in your upcoming interview, and hopefully you'll land the job. Peace!

  • @gauravagrawal7988
    @gauravagrawal798810 ай бұрын

    i think wrong solution consider a case s="azbcabc" t = "abc" this solution will give answer "azbc" but right answer you know man just check

  • @codingatascale4927

    @codingatascale4927

    10 ай бұрын

    I linked the code on my github account, you can try it yourself, so many come here and say the solution doesn’t work, either without watching the full video or not trying the code.