Sliding Window Algorithm - Longest Substring Without Repeating Characters (LeetCode)

Поделиться
HTML-код
  • Опубликовано: 20 авг 2024

Комментарии • 217

  • @rahulmarkonda
    @rahulmarkonda 3 года назад +20

    Finally understood this algorithm after one full day . Great explanation:)

    • @ascar66
      @ascar66 2 года назад

      The same bro, spent almost a day to understand. )))

  • @smarchz
    @smarchz 2 года назад +14

    Fantastic animation. It makes the explanation clearer and better.

  • @elooser
    @elooser 2 года назад +3

    literally THE BEST explanation on RUclips for this LC question. Thank you, Michael

  • @srinadhp
    @srinadhp 3 года назад +9

    Was confused a lot with this problem.. You made it very simple. Thank you so much! Keep up the good work.

  • @swathiupadhyaya8812
    @swathiupadhyaya8812 3 года назад +13

    Clean and concise explanation, very helpful. Thank you :) Never understood the concept of the sliding window better! Kudos! (y)

    • @AlgosWithMichael
      @AlgosWithMichael  3 года назад

      Awesome, im so glad the video helped you!

    • @pandu7820
      @pandu7820 2 года назад

      @@AlgosWithMichael can you code this in python

    • @rohitkeshri2451
      @rohitkeshri2451 Год назад

      @@pandu7820 class Solution:
      def lengthOfLongestSubstring(self, s: str):
      if len(s) == 0 or s == None:
      return 0
      i = 0
      j = 0
      max_value = 0
      set1 = set()
      set1 = set()
      while i < len(s):
      char = s[i]
      while char in set1:
      set1.remove(s[j])
      j = j + 1
      set1.add(char)
      max_value = max(max_value, i - j + 1)
      i = i + 1
      return max_value
      p = Solution()
      s = "pwwkew"
      result = p.lengthOfLongestSubstring(s)
      print(result)

  • @didactics740
    @didactics740 3 года назад +4

    Thanku so much bro! I watched multiple videos to try and understand the algorithm of this problem and gotta say your video was the most helpful atm

    • @javaguru7453
      @javaguru7453 2 года назад

      ruclips.net/user/maksrane100 check this too

  • @waynegreen7970
    @waynegreen7970 4 года назад +4

    Wow! This explanation really resonated with me. Not to sound overly dramatic, but for me, this was a great programming performance.
    Excellent "whiteboard" explanation, along with the followup explanation while coding to reinforce what was discussed on the whiteboard.
    All accomplished in about 10 minutes.

    • @AlgosWithMichael
      @AlgosWithMichael  4 года назад +1

      Thank you very much! I'm glad you got some use out of it, since most of us (including myself) are visual learners, it makes the most sense to have full animated examples.

  • @matteopreda7842
    @matteopreda7842 11 месяцев назад +1

    the double while loop increase the time complexity to O(N2), the concept behind the sliding window approach is to slow down the finding of substring. If you have a string of 1000 charcater it increase its time complexity to 1000 * 1000 = 1000000

    • @matteopreda7842
      @matteopreda7842 11 месяцев назад

      function findLongestSubstring(str) {
      let longest = 0;
      let seen = {};
      let start = 0;

      for (let i = 0; i < str.length; i++) {
      let char = str[i];
      if (seen[char]) {
      start = Math.max(start, seen[char]);
      }
      longest = Math.max(longest, i - start + 1);
      seen[char] = i + 1;
      }
      return longest;
      }
      this is write in javascript and have time complexity of O(N) which means that the time complexity of the algorithm is linear (with the number of char in the string)

  • @Ronnyhlim
    @Ronnyhlim 3 года назад +4

    The video editing made this extremely enjoyable and easy to follow. Thumbs up from me

  • @sarahsharma9078
    @sarahsharma9078 2 года назад +2

    Thank you so much. I have seen almost 10 videos on understanding this question but this was the best explanation for this question.

  • @haacki4720
    @haacki4720 Год назад

    That's the best explanation which I found on youtube

  • @0anant0
    @0anant0 4 года назад +2

    Thanks! It is important to understand that when a repeating char is found (e.g. i = 2, ch = second 'W'), all chars in the current SW that appear before this repeating char also have to be removed (and then we remove the repeating char).
    However, the whole SW is not erased -- the chars after the repeating char (that was removed) still remain in SW.
    e.g. if there was a 'A' between the two W's, then the SW will change from "PWA" to "AW".

  • @lumsism
    @lumsism 3 года назад +2

    Top notch algorithm solution explanation! Could not have asked for any better. Be blessed!

  • @275phuongvy
    @275phuongvy 3 года назад +1

    the new video style with animation makes the problem be easier to understand. Thank you

  • @akshaykumar-di4tj
    @akshaykumar-di4tj 3 года назад +3

    Your videos are very intuitive and easy to understand. I really appreciate your work dude.

  • @SoyDelSouth
    @SoyDelSouth 3 года назад

    First time I ever heard about sliding windows. I was completely clueless. This content is subscription worthy, thanks!

  • @felipemelendez5741
    @felipemelendez5741 2 года назад

    Best sliding window explanation in the internet, please make more algorithm explanation videos.

  • @jonathansaraco
    @jonathansaraco 2 года назад

    Got this question on an interview. I knew sliding window was the approach, but I couldn't get it in time. I never thought to implement it with two pointers as you did here. Thankfully I've moved onto the next stage of the hiring process, but if I ever see this problem again, now I'll know an easy solution! Thanks.

  • @carlosalarcon2737
    @carlosalarcon2737 Год назад

    By using diagrams is very simple to understand...many thanks!

  • @jigyasupant453
    @jigyasupant453 2 года назад +1

    Highly appreciate the way you made this algorithm so easy. Commendable job. Thanks !

  • @YashTrivedi21
    @YashTrivedi21 4 года назад +2

    Thank you so much, I was waiting for this technique for so long.

  • @lapujain
    @lapujain 3 года назад

    The most underrated technical channel, you are doing a awesome awesome awesome job men...

  • @prasenjitsingharoy9370
    @prasenjitsingharoy9370 3 года назад +1

    The explanation with the animation was incredibly well. Keep it up bro 👍.

  • @Metachief_X
    @Metachief_X Год назад

    you're saving lives out here man. keep up the good work!!

  • @amitbhattacharya356
    @amitbhattacharya356 2 года назад

    Finally, I understood the concept.

  • @junqichen6241
    @junqichen6241 2 года назад

    Your video on this topic helps a lot with my understanding. Thank you!

  • @alibaba888
    @alibaba888 2 года назад

    The clearest explanation I have come across so far. Thanks!

  • @haris9356
    @haris9356 Год назад

    Wow..What an Explanation ! Became Fan of you!

  • @nm_scarecrow302
    @nm_scarecrow302 3 года назад +2

    Great explanation. We could also just use set.size() to update max rather than i - j + 1. The set will always contain unique elements.

  • @pushkarkumar7173
    @pushkarkumar7173 3 года назад

    Your videos are like super. And after watching your video the fear of coding likes gone...

  • @shristypandey3733
    @shristypandey3733 3 года назад

    This is the best video of sliding window algorithm in youtube.Thanks alot.

  • @karanvirsagar1998
    @karanvirsagar1998 2 года назад

    Thankyou ! you got a new subscriber,
    I was just exploring the explanation from last 6 hours
    and finally, your video helped me a lot...
    tysm !!!!

  • @DriveBreakFixRepeat
    @DriveBreakFixRepeat 2 года назад

    Great solution. Logic was super easy to follow and understand where you were going.

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 3 года назад +1

    the explanation is just brilliant, thank you!!!

    • @javaguru7453
      @javaguru7453 2 года назад

      ruclips.net/user/maksrane100 check this too

  • @lennythach607
    @lennythach607 2 года назад

    Absolutely brilliant Michael!

  • @ashishpandey698
    @ashishpandey698 3 месяца назад

    Thanks for giving a clean solution!!

  • @sikaili99
    @sikaili99 2 года назад

    Best explanation about sliding window,,, thanks man 😎

  • @vaibhavsahastrabuddhe173
    @vaibhavsahastrabuddhe173 7 месяцев назад

    thank you for explaining it was very helpful please make more such videos. thanks again.

  • @TheMcallist1
    @TheMcallist1 3 года назад +1

    Great stuff, thanks. The animation makes it so much easier to grasp.

  • @jorgegonzaloalfaro5378
    @jorgegonzaloalfaro5378 3 года назад +1

    bruh.. make more videos like this and i'll like and comment on every single one! Thank you!

  • @DJSamijon
    @DJSamijon 3 года назад

    so much better than the other videos for this problem

  • @adi94071
    @adi94071 3 года назад

    dude you are awesome! you always have the best explanation for any problem as compared to other youtube videos.

  • @nileshpatil3710
    @nileshpatil3710 2 года назад

    clean, smooth and easy explanation.

  • @debanjanchatterjee9007
    @debanjanchatterjee9007 4 года назад +2

    Loving your content. Thank you for making these videos!

    • @AlgosWithMichael
      @AlgosWithMichael  4 года назад

      My pleasure!

    • @jabaricallan4939
      @jabaricallan4939 3 года назад

      I know im asking randomly but does any of you know of a tool to log back into an Instagram account??
      I somehow forgot the password. I would appreciate any tricks you can give me

  • @kunal_chand
    @kunal_chand 4 года назад

    Super clear with your animated explanation.

  • @marineoceania
    @marineoceania 2 года назад

    Incredibly helpful and easy to understand. THANK YOU!

  • @owenkhoury6756
    @owenkhoury6756 3 года назад

    You're awesome man. clearest explanations I've seen

  • @StyleTrick
    @StyleTrick 3 года назад +1

    Once again, great explanation and the demo before really helps a lot!

  • @rishijuvekar7572
    @rishijuvekar7572 3 года назад

    Best Explanation !

  • @vicks789
    @vicks789 2 года назад

    You are just Amazing. You made it so simple for every one to understand. You deserve more subscribers :-)

  • @nosthrillz
    @nosthrillz 2 года назад

    This was really good, well done dude!

  • @aby.t
    @aby.t 2 года назад

    Thanks! I like this explanation a lot more than the one in Grokking the Coding Interview and it was illustrated quite nicely. Gave a like.

  • @varunishamarao5018
    @varunishamarao5018 3 года назад

    Amazing explanation. Best video about sliding window.

  • @kibirigejohn8324
    @kibirigejohn8324 Год назад

    Thank you Micheal

  • @AlexLancer
    @AlexLancer 2 года назад

    Great explanation. Thanks

  • @himansurful
    @himansurful 2 года назад

    Great explanation. You make difficult things look easy

  • @jddesignzs9874
    @jddesignzs9874 3 года назад

    Awesomely simple approach.. I love your explanations.... liked --subscribed and looking forward for more like this..

  • @akhilbhardwaj7733
    @akhilbhardwaj7733 3 года назад

    Nice explanation and thanks for describing time and space complexity.

  • @shasvatnayak2329
    @shasvatnayak2329 3 года назад

    Best explanation seen so far, thanks for video :)

  • @AlexN2022
    @AlexN2022 2 года назад

    I don't see how it is linear. When we have a conflict - found a repeated character - we need to trim the prefix of the string from our set until the conflict goes away. That takes n1*O(1), where n1 < N is the length of the prefix being removed. (Ignoring for now the fact that worst case for removal from set if log(size))
    And we need to do it as many times as there are conflicts. Which is n2 < N. Which in total still gives us O(N**2).

    • @AlexN2022
      @AlexN2022 2 года назад

      I was wrong. At max, we only add s.size() = N characters to the set, and we can only remove at most N. So as long as set removal is assumed to be O(1), total complexity is O(N).

  • @Leo-jz3tu
    @Leo-jz3tu 2 года назад

    nice one bro, subscribed

  • @nullptryt
    @nullptryt 2 года назад

    this was exactly what i needed thank you! immediately subscribed :)

  • @DavidMartinez-vz6zt
    @DavidMartinez-vz6zt 3 года назад

    This is the best explanation I have seen, Thank you very much!! I'm Subscribing

  • @hdgntvmtv
    @hdgntvmtv 2 года назад

    Very useful video. Thank you bro

  • @user-cw3jg9jq6d
    @user-cw3jg9jq6d 5 месяцев назад

    I really like your explanations and that you code in Java.

  • @psychedeliccoffee2737
    @psychedeliccoffee2737 2 года назад

    awesome video. Thanks for the great explanation

  • @harshaggarwal1967
    @harshaggarwal1967 2 года назад

    Nice explanation.. Thanks!!!

  • @miguelguzman8217
    @miguelguzman8217 Год назад

    Great explanation, really easy to follow, thanks for your content and help :)

  • @hamzaibrahim49
    @hamzaibrahim49 2 года назад

    awesome video man! explained it so well thank you!

  • @vinayryuzakikale7247
    @vinayryuzakikale7247 2 года назад

    Accurate explanation...Thanks a lot

  • @rajeshb1504
    @rajeshb1504 4 года назад

    It was very good one and easy to understand the problem . It helped a lot.

  • @erickjhormanromero6905
    @erickjhormanromero6905 2 года назад

    Amazing explanation. Thanks

  • @sjwang3892
    @sjwang3892 3 года назад

    Super clear explanation! Thanks

    • @AlgosWithMichael
      @AlgosWithMichael  3 года назад

      Glad it was helpful!

    • @sjwang3892
      @sjwang3892 3 года назад

      @@AlgosWithMichael I watched this yesterday and was asked about the sliding window algorithm in today's interview.. ?!

  • @zhenliu4779
    @zhenliu4779 2 года назад

    thank you. Great explanation

  • @shripadkhandare
    @shripadkhandare 5 месяцев назад

    visualization helps. thanks !

  • @TheLucidWay
    @TheLucidWay 2 года назад +2

    Superb breakdown. I was able to follow everything up til the time & space complexity part. Could someone please explain why it's not O(n^2). I know he explained it, but it's just not clicking for me. He described it well, but I can be kinda slow at times.

    • @Akel4611
      @Akel4611 2 года назад +1

      Nice explanation . very useful . Thank you . Please explain , how can a while loop inside a while loop did not cause O(n^2) time complexity . really confused at that part .
      Jason
      Jason
      3 months ago
      The inner while loop will only iterate once through the every char of the string. It does not keep looping for every iteration of the outer loop. Hence it is O(2N). Hope it helps!
      TJ Tolton
      TJ Tolton
      1 month ago
      a critical clue about the time complexity here: when j advances forward, i does NOT go back to j and start iterating from there. it stays where it is -- otherwise it would be O(n^2)
      from older comments :)

    • @TheLucidWay
      @TheLucidWay 2 года назад +1

      @@Akel4611 Okay, I get it now. Thanks for showing me these comments.

  • @soulboy2223
    @soulboy2223 3 года назад

    love your solution, easy to understand!

  • @hazemhashem3791
    @hazemhashem3791 3 года назад

    great content keep it up, buddy!

  • @ashwithchandra2622
    @ashwithchandra2622 Год назад

    excellent animation easy understanding very welll....

  • @s750716
    @s750716 4 года назад

    Thanks for making videos with nice explanations.

  • @DsaDecoder
    @DsaDecoder Год назад

    Thanks Man!!

  • @gitanshugupta8556
    @gitanshugupta8556 3 года назад

    Awesome explanation bro

  • @sandeepa4116
    @sandeepa4116 3 года назад

    Awesome explanation. Thank you

  • @tehleelmir4407
    @tehleelmir4407 2 года назад

    thank you, i wrote the code for it b4 but it took n^2 time
    HashMap map = new HashMap();
    int top = 0, x = 0;

    for(int i=0; i

  • @amanbhayani65
    @amanbhayani65 3 года назад

    This was a great explanation! Thank you!

  • @vidhya821
    @vidhya821 2 года назад

    Subscribed!

  • @expansivegymnast1020
    @expansivegymnast1020 2 года назад

    Great video.

  • @kurmianil1
    @kurmianil1 3 года назад

    cool explanation

  • @tjalferes
    @tjalferes 2 года назад

    Thank you.

  • @nhatlequang3363
    @nhatlequang3363 3 года назад

    Thanks!

  • @MsUltraKawaii
    @MsUltraKawaii 3 года назад +1

    Why can't we calculate max base on Math.max(max, set.length) instead of i-i+1?

    • @AlgosWithMichael
      @AlgosWithMichael  3 года назад

      You can do it that way as well, it is up to you!

  • @jiaxuanzhang2452
    @jiaxuanzhang2452 3 года назад

    Thank you this is so helpful!

  • @giulcg
    @giulcg Год назад

    best explanation ;)

  • @srinadhp
    @srinadhp 3 года назад

    Wish you could build 424. Longest Repeating Character Replacement problem with the same approach you described here.

  • @madhav3444
    @madhav3444 3 года назад +2

    Great explanation ! What software do you use to make these animation ? Thanks

    • @زانغت_ساما
      @زانغت_ساما 3 года назад +1

      u can use adobe premiere pro of filmora

    • @madhav3444
      @madhav3444 3 года назад

      @@زانغت_ساما thank you ❤️

  • @sakshisingh9706
    @sakshisingh9706 4 года назад

    amazing explanation . thankyou so much:)

  • @mnchester
    @mnchester 2 года назад

    great vid

  • @user-qc4zu5ti6h
    @user-qc4zu5ti6h Год назад

    Thank you for the video. I am simply curious about what is the underlying time complexity for function set.contains() and set.remove()

  • @WhisperingBengal
    @WhisperingBengal 3 года назад

    you are the best

  • @shenth27
    @shenth27 2 года назад

    Wonder why you need the inner while loop, I thought hashset wouldn't have a duplicate so no need to remove within a loop.