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

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ม.ค. 2025

ความคิดเห็น • 219

  • @smarchz
    @smarchz 2 ปีที่แล้ว +15

    Fantastic animation. It makes the explanation clearer and better.

  • @rahulmarkonda
    @rahulmarkonda 3 ปีที่แล้ว +24

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

    • @ascar66
      @ascar66 2 ปีที่แล้ว

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

  • @domdom_hello
    @domdom_hello 2 ปีที่แล้ว +3

    literally THE BEST explanation on TH-cam 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.

  • @sarahsharma9078
    @sarahsharma9078 3 ปีที่แล้ว +2

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

  • @matteopreda7842
    @matteopreda7842 ปีที่แล้ว +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 ปีที่แล้ว

      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)

  • @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 3 ปีที่แล้ว

      th-cam.com/users/maksrane100 check this too

  • @lumsism
    @lumsism 3 ปีที่แล้ว +2

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

  • @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.

  • @swathiupadhyaya8812
    @swathiupadhyaya8812 4 ปีที่แล้ว +13

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

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Awesome, im so glad the video helped you!

    • @pandu7820
      @pandu7820 2 ปีที่แล้ว

      @@AlgosWithMichael can you code this in python

    • @rohitkeshri2451
      @rohitkeshri2451 2 ปีที่แล้ว

      @@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)

  • @Ronnyhlim
    @Ronnyhlim 3 ปีที่แล้ว +5

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

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +1

      Much appreciated!

    • @ellenkganakga
      @ellenkganakga หลายเดือนก่อน

      @@AlgosWithMichael Thank you for your hard work ! :)

  • @akshaykumar-di4tj
    @akshaykumar-di4tj 4 ปีที่แล้ว +3

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

  • @felipemelendez5741
    @felipemelendez5741 3 ปีที่แล้ว

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

  • @lennythach607
    @lennythach607 2 ปีที่แล้ว

    Absolutely brilliant Michael!

  • @SoyDelSouth
    @SoyDelSouth 3 ปีที่แล้ว

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

  • @275phuongvy
    @275phuongvy 4 ปีที่แล้ว +1

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

  • @jigyasupant453
    @jigyasupant453 3 ปีที่แล้ว +1

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

  • @Metachief_X
    @Metachief_X 2 ปีที่แล้ว

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

  • @DriveBreakFixRepeat
    @DriveBreakFixRepeat 2 ปีที่แล้ว

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

  • @alibaba888
    @alibaba888 2 ปีที่แล้ว

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

  • @lapujain
    @lapujain 3 ปีที่แล้ว

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

  • @karanvirsagar1998
    @karanvirsagar1998 3 ปีที่แล้ว

    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 !!!!

  • @prasenjitsingharoy9370
    @prasenjitsingharoy9370 3 ปีที่แล้ว +1

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

  • @YashTrivedi21
    @YashTrivedi21 4 ปีที่แล้ว +2

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

  • @jonathansaraco
    @jonathansaraco 3 ปีที่แล้ว

    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.

  • @haacki4720
    @haacki4720 2 ปีที่แล้ว

    That's the best explanation which I found on youtube

  • @svalyavasvalyava9867
    @svalyavasvalyava9867 3 ปีที่แล้ว +1

    the explanation is just brilliant, thank you!!!

    • @javaguru7453
      @javaguru7453 3 ปีที่แล้ว

      th-cam.com/users/maksrane100 check this too

  • @junqichen6241
    @junqichen6241 2 ปีที่แล้ว

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

  • @shristypandey3733
    @shristypandey3733 4 ปีที่แล้ว

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

  • @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".

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Exactly, nice explanation.

    • @lumsism
      @lumsism 3 ปีที่แล้ว

      How's that implemented?

  • @carlosalarcon2737
    @carlosalarcon2737 2 ปีที่แล้ว

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

  • @TheMcallist1
    @TheMcallist1 4 ปีที่แล้ว +1

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

  • @haris9356
    @haris9356 ปีที่แล้ว

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

  • @sikaili99
    @sikaili99 2 ปีที่แล้ว

    Best explanation about sliding window,,, thanks man 😎

  • @nileshpatil3710
    @nileshpatil3710 3 ปีที่แล้ว

    clean, smooth and easy explanation.

  • @ashishpandey698
    @ashishpandey698 8 หลายเดือนก่อน

    Thanks for giving a clean solution!!

  • @DJSamijon
    @DJSamijon 4 ปีที่แล้ว

    so much better than the other videos for this problem

  • @vaibhavsahastrabuddhe173
    @vaibhavsahastrabuddhe173 ปีที่แล้ว

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

  • @marineoceania
    @marineoceania 2 ปีที่แล้ว

    Incredibly helpful and easy to understand. THANK YOU!

  • @adi94071
    @adi94071 4 ปีที่แล้ว

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

  • @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

  • @owenkhoury6756
    @owenkhoury6756 3 ปีที่แล้ว

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

  • @kunal_chand
    @kunal_chand 4 ปีที่แล้ว

    Super clear with your animated explanation.

  • @amitbhattacharya356
    @amitbhattacharya356 3 ปีที่แล้ว

    Finally, I understood the concept.

  • @vicks789
    @vicks789 2 ปีที่แล้ว

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

  • @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.

  • @nosthrillz
    @nosthrillz 2 ปีที่แล้ว

    This was really good, well done dude!

  • @StyleTrick
    @StyleTrick 3 ปีที่แล้ว +1

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

  • @jddesignzs9874
    @jddesignzs9874 3 ปีที่แล้ว

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

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว +1

      More videos to come, thanks for subscribing!

  • @akhilbhardwaj7733
    @akhilbhardwaj7733 4 ปีที่แล้ว

    Nice explanation and thanks for describing time and space complexity.

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Of course, it is very important to know!

  • @jorgegonzaloalfaro5378
    @jorgegonzaloalfaro5378 4 ปีที่แล้ว +1

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

  • @rishijuvekar7572
    @rishijuvekar7572 3 ปีที่แล้ว

    Best Explanation !

  • @HologramJay
    @HologramJay 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 ปีที่แล้ว +2

      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 :)

    • @HologramJay
      @HologramJay 2 ปีที่แล้ว +1

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

  • @nullptryt
    @nullptryt 2 ปีที่แล้ว

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

  • @kibirigejohn8324
    @kibirigejohn8324 2 ปีที่แล้ว

    Thank you Micheal

  • @hamzaibrahim49
    @hamzaibrahim49 3 ปีที่แล้ว

    awesome video man! explained it so well thank you!

  • @miguelguzman8217
    @miguelguzman8217 2 ปีที่แล้ว

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

  • @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.. ?!

  • @harshaggarwal1967
    @harshaggarwal1967 2 ปีที่แล้ว

    Nice explanation.. Thanks!!!

  • @himansurful
    @himansurful 3 ปีที่แล้ว

    Great explanation. You make difficult things look easy

  • @hdgntvmtv
    @hdgntvmtv 2 ปีที่แล้ว

    Very useful video. Thank you bro

  • @soulboy2223
    @soulboy2223 4 ปีที่แล้ว

    love your solution, easy to understand!

  • @psychedeliccoffee2737
    @psychedeliccoffee2737 2 ปีที่แล้ว

    awesome video. Thanks for the great explanation

  • @shasvatnayak2329
    @shasvatnayak2329 4 ปีที่แล้ว

    Best explanation seen so far, thanks for video :)

  • @Leo-jz3tu
    @Leo-jz3tu 3 ปีที่แล้ว

    nice one bro, subscribed

  • @DavidMartinez-vz6zt
    @DavidMartinez-vz6zt 4 ปีที่แล้ว

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

  • @rajeshb1504
    @rajeshb1504 4 ปีที่แล้ว

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

  • @zhenliu4779
    @zhenliu4779 3 ปีที่แล้ว

    thank you. Great explanation

  • @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.

  • @hazemhashem3791
    @hazemhashem3791 3 ปีที่แล้ว

    great content keep it up, buddy!

  • @sandeepa4116
    @sandeepa4116 3 ปีที่แล้ว

    Awesome explanation. Thank you

  • @vinayryuzakikale7247
    @vinayryuzakikale7247 2 ปีที่แล้ว

    Accurate explanation...Thanks a lot

  • @shripadkhandare
    @shripadkhandare 10 หลายเดือนก่อน

    visualization helps. thanks !

  • @ashwithchandra2622
    @ashwithchandra2622 2 ปีที่แล้ว

    excellent animation easy understanding very welll....

  • @gitanshugupta8556
    @gitanshugupta8556 3 ปีที่แล้ว

    Awesome explanation bro

  • @s750716
    @s750716 4 ปีที่แล้ว

    Thanks for making videos with nice explanations.

  • @amanbhayani65
    @amanbhayani65 4 ปีที่แล้ว

    This was a great explanation! Thank you!

    • @AlgosWithMichael
      @AlgosWithMichael  4 ปีที่แล้ว

      Glad you enjoyed it!

    • @javaguru7453
      @javaguru7453 3 ปีที่แล้ว

      th-cam.com/users/maksrane100 check this too

  • @jiaxuanzhang2452
    @jiaxuanzhang2452 3 ปีที่แล้ว

    Thank you this is so helpful!

  • @ShahinAnsari-e6s
    @ShahinAnsari-e6s 10 หลายเดือนก่อน

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

  • @raghavendrareddy8242
    @raghavendrareddy8242 2 ปีที่แล้ว

    Nice one. small doubt contains method not counted in big O ?

  • @pushkarkumar7173
    @pushkarkumar7173 3 ปีที่แล้ว

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

  • @DsaDecoder
    @DsaDecoder 2 ปีที่แล้ว

    Thanks Man!!

  • @Kuan-ChihWang
    @Kuan-ChihWang ปีที่แล้ว

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

  • @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).

  • @tjalferes
    @tjalferes 2 ปีที่แล้ว

    Thank you.

  • @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 ❤️

  • @turtlesarecool14
    @turtlesarecool14 2 ปีที่แล้ว

    When/where does the passed string s get placed inside hashset 'set'?

  • @srinadhp
    @srinadhp 3 ปีที่แล้ว

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

  • @nhatlequang3363
    @nhatlequang3363 3 ปีที่แล้ว

    Thanks!

  • @sakshisingh9706
    @sakshisingh9706 4 ปีที่แล้ว

    amazing explanation . thankyou so much:)

  • @expansivegymnast1020
    @expansivegymnast1020 2 ปีที่แล้ว

    Great video.

  • @vidhya821
    @vidhya821 2 ปีที่แล้ว

    Subscribed!

  • @akshaysingh548
    @akshaysingh548 2 ปีที่แล้ว

    set are not index based in cpp, any idea how to implement this in cpp?

  • @ajaym9360
    @ajaym9360 3 ปีที่แล้ว

    Hi
    Instead of creating a set for storing elements
    We can create an character array?

    • @AlgosWithMichael
      @AlgosWithMichael  3 ปีที่แล้ว

      Technically yes, but a set is better because the lookup with be O(1).

  • @kurmianil1
    @kurmianil1 4 ปีที่แล้ว

    cool explanation

  • @jabellae1
    @jabellae1 2 ปีที่แล้ว

    what would be the difference between set.size for max and i - j + 1 ?? why not just if statement if max is smaller than set.size then update max?

  • @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.

  • @rajkumarjannu5404
    @rajkumarjannu5404 ปีที่แล้ว

    8 th line While(set.contains(c)) in a set[p ] is there conduction true

  • @satya.bodapati
    @satya.bodapati 3 ปีที่แล้ว

    Do you need the second while loop? Just the if(set.contains(c)) is sufficient right?

    • @shenth27
      @shenth27 3 ปีที่แล้ว

      That's right. You can just remove the jth element and increment j in the else block, should work.