L3. Longest Substring Without Repeating Characters | 2 Pointers and Sliding Window Playlist

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 145

  • @kuldeepdixit1006
    @kuldeepdixit1006 7 місяців тому +117

    whenever I am looking for an explanation to a problem and I search it on youtube and if I don't find a solution by striver I get disappointed...Love from Flipkart!

  • @shwetanshu13
    @shwetanshu13 8 місяців тому +36

    The new look of the DSA sheet and the whole website is just way too awesome sir.

  • @pavitrasingh7833
    @pavitrasingh7833 8 місяців тому +9

    I just cant express my gratitude in words towards this man . the way he simplifies every single thing makes everything appear so easy . I am so glad that I found this channel.

  • @simple_user000
    @simple_user000 7 місяців тому +16

    I solved it on my own! Your teaching is amazing bhaiya!!!

  • @hashcodez757
    @hashcodez757 3 місяці тому +17

    Hello Bhaiya!!
    From past few weeks i was not able to watch any of your videos.
    Was going through depression .
    But the moment i joined the journey again with you is a blessing.
    You have a different aura ....just looking at you i just simply forget everything and a new spark is ignited within me.
    Thank alot Bhaiya for your efforts and helping me!!

    • @sahil_bagde
      @sahil_bagde 2 місяці тому +1

      Just hope for the better friend , I am gone through that🙂😀

  • @tanvirahammed3783
    @tanvirahammed3783 8 місяців тому +101

    Hi Striver, I'm not sure if this comment will reach you or not, but I just want to say one thing: I've been watching your videos for more than a year now and have covered a huge part of the graph and dynamic programming playlists. Whenever I watch your videos, I really fall in love with your teaching style, and of course, your smile, and sometimes your small jokes. I enjoy your videos, and it feels like you're not just my tutor; it feels like you're someone very close to me, teaching me with fun and pleasure. However, in your recent playlists, I totally miss that. It feels like you're not as friendly anymore; you're just a teacher like any other tutor. Whenever I watch videos from this playlist, sometimes I wonder what happened to you. Why are you so quiet now? Why don't you joke around anymore? After all, I love your work; it's just my opinion.

    • @parth_3856
      @parth_3856 7 місяців тому +5

      yeh! i noticed it too! hoping he's doing good in life..

    • @sumitgupta310
      @sumitgupta310 5 місяців тому

      agree

    • @rushidesai2836
      @rushidesai2836 4 місяці тому +8

      Google's job is stressful buddy. It takes lot of effort jusy to make these videos.

    • @Goku-dk4mr
      @Goku-dk4mr Місяць тому

      @@rushidesai2836 lol, pay me 1 cr per annum, I am ready to take the stress lol, millions of middle class people are earning peanuts but still work with smile on their face just to support their families

    • @Goku-dk4mr
      @Goku-dk4mr Місяць тому

      @@parth_3856 with job in google everyone will do good in life

  • @shivamjaiswal1263
    @shivamjaiswal1263 5 місяців тому +19

    hi Striver,
    Your Graph, DP, binary search playlist are amazing. Please create a playlist for String as well.

    • @rushidesai2836
      @rushidesai2836 4 місяці тому

      Binary Search was pretty good I agree!

  • @harshvishvakarma2071
    @harshvishvakarma2071 8 місяців тому +9

    Need such type of teachers

  • @AshutoshAnand-o5l
    @AshutoshAnand-o5l 6 місяців тому +10

    1 fix in the brute force is that you need to fill the arrays with 0 every time youre moving i. so heres the correctedd brute.public int lengthOfLongestSubstring(String s) {
    int[] arr = new int[256];
    int max = 0;
    int n = s.length();

    for (int i = 0; i < n; i++) {
    // Reset the array for each new starting point
    Arrays.fill(arr, 0);

    for (int j = i; j < n; j++) {
    // If character at j has been seen, break the loop
    if (arr[s.charAt(j)] == 1) {
    break;
    }

    // Otherwise, add the character to substring
    int len = j - i + 1;
    max = Math.max(len, max);
    // Remember it
    arr[s.charAt(j)] = 1;
    }
    }
    return max;
    }

    • @SibiRanganathL
      @SibiRanganathL 3 місяці тому

      can i use HashMap instead of hash Array since TC for searching in HashMap is also O(1)

    • @AshutoshAnand-o5l
      @AshutoshAnand-o5l 3 місяці тому +1

      @@SibiRanganathL yes sure!

  • @monikayadav-wb6pu
    @monikayadav-wb6pu 8 місяців тому +6

    East and west striver bhaiya is best ❤❤

  • @divyanshushukla6981
    @divyanshushukla6981 7 місяців тому +6

    Loved your explaination. Bhaiya instead of using a HashMap we can also use a Set. It will save some more memory.
    class Solution {
    public int lengthOfLongestSubstring(String s) {
    int i = 0, j = 0, max = 0;
    Set set = new HashSet();
    while (j < s.length())
    {
    if (!set.contains(s.charAt(j)))
    {
    set.add(s.charAt(j));
    j++;
    max = Math.max(max, set.size());
    }
    else
    {
    set.remove(s.charAt(i));
    i++;
    }
    }
    return max;
    }
    }

  • @adilkevin6220
    @adilkevin6220 8 місяців тому +25

    I think you haven't attached this youtube link in your take youforward site.

  • @59_sujatachandra76
    @59_sujatachandra76 8 місяців тому +5

    You are unstoppable... 🙏🙏 You are the best 🙏🙏

  • @aniketkumar2502
    @aniketkumar2502 4 місяці тому +3

    int lengthOfLongestSubstring(string s)
    {
    if(s.size()==0)return 0;
    int i=0;
    int j=0;
    int maxi=1;
    int n=s.size();
    unordered_mapmp;
    while(j=i)
    {
    i=mp[s[j]]+1;
    }
    maxi=max(maxi,j-i+1);
    mp[s[j]]=j;
    j++;
    }
    return maxi;
    }
    finally accepted.
    Thankyou Striver😊for your effort.
    i like and appreciate you from bottom of my heart.❤

  • @AyushEditz-hs6pf
    @AyushEditz-hs6pf 2 місяці тому +2

    i think this is overcomplicating things. The easy code could be:
    int n = s.length();
    int left = 0;
    int right = 0;
    int maxLen = 0;
    map mpp;
    int len=0;
    while (right < n) {
    if (mpp.find( s[right] ) != mpp.end() ) {
    left++;
    right=left;
    mpp.clear();
    }
    else{
    len++;
    mpp[s[right]] = right;
    maxLen=max(right-left+1,maxLen);
    right++;
    }
    }
    return maxLen;

  • @theee_1
    @theee_1 5 місяців тому

    Very,very smart case where left has to be the rightmost. eg checking in abc, if left is already at 4 and prior presence of b is at 2, we need not update left to 3, instead it should be max of prior instance+1,already present left

  • @riteshbisht94
    @riteshbisht94 8 місяців тому +3

    Great bhaiji 🙏

  • @RogithRog
    @RogithRog 6 місяців тому +1

    public static void main( String[] args )
    {
    String s ="pwwkew";
    String count ="";
    int max =0;
    int count1=0;
    for(int i=0;i

  • @JunaidShareef-j4u
    @JunaidShareef-j4u 21 день тому

    Explained very well... 💯💯

  • @PrinceKumar-ef1xf
    @PrinceKumar-ef1xf 5 місяців тому +2

    00:06 Finding longest substring without repeating characters
    02:46 Using 2 Pointers for Substring Generation
    04:59 Using hashing to find the longest substring without repeating characters
    07:36 Optimizing algorithm using two pointers and sliding window approach
    10:02 Understanding two pointer and sliding window approach
    12:17 Determine longest substring without repeating characters using hashmap and sliding window
    14:45 Updating characters in a sliding window to find longest substring without repeats.
    17:06 Sliding window technique for finding longest substring without repeating characters.
    19:10 Algorithm explanation and time complexity analysis
    21:42 Explanation of sliding window algorithm with two pointer

  • @technologicalvivek7510
    @technologicalvivek7510 8 місяців тому +18

    Bhaiya mene isko freq array banakar easy way me kardiya.
    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    vector freq(256, 0);

    int i = 0, j = 0;
    int maxi = 0;
    int n = s.length();

    while (j < n) {
    if (freq[s[j]] == 0) {
    freq[s[j]]++;
    maxi = max(maxi, j - i + 1);
    j++;
    } else {
    freq[s[i]]--;
    i++;
    }
    }
    return maxi;
    }
    };

    • @NEUTRON-h5t
      @NEUTRON-h5t 5 місяців тому +2

      bro can you explain me this code especially this part
      else {
      freq[s[i]]--;
      i++;

    • @ayaamverma6742
      @ayaamverma6742 2 місяці тому +1

      @@NEUTRON-h5t when you find that there is a duplicate element, i.e. freq[s[j]] != 0, you start moving the i pointer till that duplicate element is removed from your window. By the time you reach 1 index ahead of the duplicate element, you would have removed the duplicate element. Again, do a dry run to better understand the logic.

  • @sathishyenuganti4859
    @sathishyenuganti4859 3 місяці тому

    I Always love your explanation ❤

  • @heershah239
    @heershah239 6 місяців тому +5

    with this solution we can make time complexity as O(n) and space complexity O(n)
    Set set=new HashSet();

    int left=0;
    int right=0;
    int mxLen=0;
    String result = "";
    while(rightmxLen) {
    mxLen= right- left +1;
    result=str.substring(left,right+1);
    }
    right++;
    }else {
    set.remove(str.charAt(left));
    left++;

    }
    }
    return result;
    }

  • @ManishKumar-dk8hl
    @ManishKumar-dk8hl 8 місяців тому +6

    Bhaiya wala code { BY USING MAP NAAM KI ARRAY } :--
    class Solution {

    public int lengthOfLongestSubstring(String s) {
    int l=0;
    int r=0;
    int[] map=new int[256];
    Arrays.fill(map, -1);
    int maxlen=0;
    while(r

  • @Divyanshu-q5n
    @Divyanshu-q5n 2 місяці тому

    vector< int > mpp(256,-1);
    int l=0;
    int r=0;
    int maxlength=0;
    while(r=l) l = mpp[s[r]] +1;
    }

    mpp[s[r]] = r;
    maxlength = max(maxlength,r-l+1);
    r++;
    }
    return maxlength;

  • @Learner_45
    @Learner_45 3 місяці тому +1

    With set
    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    int n=s.size();
    if(n==0) return 0;
    int l=0,r=0;
    int ans=1;
    sets1;
    while(r

  • @amanbhatt1087
    @amanbhatt1087 7 місяців тому +3

    for those who are struggling use memset to initialise hash array instead of normal initialization
    ```
    int lengthOfLongestSubstring(string s) {
    int n = s.length(), maxlen = 0;
    int l = 0, r = 0;
    int hash[255];
    memset(hash, -1, sizeof(hash));
    while (r < n) {
    if (hash[s[r]] != -1 && hash[s[r]] >= l) {
    l = hash[s[r]] + 1;
    }
    hash[s[r]] = r;
    maxlen = max(maxlen, r - l + 1);
    r++;
    }
    return maxlen;
    }
    ```

    • @suhanigupta2861
      @suhanigupta2861 5 місяців тому +1

      What is a memset?

    • @suhanigupta2861
      @suhanigupta2861 5 місяців тому +1

      Why does a normal intialization not work on this code?

    • @Messi23485
      @Messi23485 3 місяці тому

      @@suhanigupta2861 Have you got to know why? If yes please explain

    • @suhanigupta2861
      @suhanigupta2861 3 місяці тому

      @@Messi23485 No idea as of now

  • @PranamHegde
    @PranamHegde 3 місяці тому

    One small addition to the code - the hashmap has to be updated with the location of the right pointer

  • @suryas8860
    @suryas8860 3 місяці тому

    Time complexity-O(n) only ,no while loop, Check my solution
    int lengthOfLongestSubstring(string s) {
    unordered_map mpp;
    int count=0;
    int max=0;
    for(int i=0;i1){
    count=min(i- mpp[s[i]].second,count);
    }
    mpp[s[i]].second=i;
    if(count>max){max=count;}
    }
    return max;
    }

  • @lokeshgodara1329
    @lokeshgodara1329 Місяць тому

    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    int l = 0;
    int r = 0;
    int maxlen = 0;
    map mpp;
    while (r < s.size()) {
    mpp[s[r]]++;
    while(mpp[s[r]]>1){
    mpp[s[l]]--;
    l++;
    }
    maxlen = max(maxlen, r - l + 1);
    r++;
    }
    return maxlen;
    }
    };

  • @U2011-n7w
    @U2011-n7w 5 місяців тому +1

    best explanation

  • @ashj1165
    @ashj1165 2 місяці тому

    with the above psuedo code, solved using hashmap:
    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    map mp;
    int maxLen = 0;
    int n = s.size();
    int l=0, r=0;
    while(r

  • @zenilkapadia7572
    @zenilkapadia7572 2 місяці тому

    Superb raj❤

  • @subee128
    @subee128 8 місяців тому +1

    Thank you very much

  • @mr_weird3680
    @mr_weird3680 8 місяців тому +1

    Thanks Brother

  • @balajik7376
    @balajik7376 7 місяців тому

    It will be more helpful if u submit in any coding platform

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 5 місяців тому

    NICE SUPER EXCELLENT MOTIVATED

  • @oyeesharme
    @oyeesharme 3 місяці тому

    understood bhaiya

  • @anilrouto4513
    @anilrouto4513 4 місяці тому +2

    Hi @takeuforward , I am not getting language specific codes in your website also. In ur website it is showing coming soon.
    Would you please update the same if u have not updated

  • @lavanyaan2158
    @lavanyaan2158 7 місяців тому +1

    Thank you so much bro

  • @raghavmanish24
    @raghavmanish24 5 місяців тому

    understood striver.....thanku

  • @codeman3828
    @codeman3828 7 місяців тому +1

    Thanks. Understood.

  • @vivekanand9492
    @vivekanand9492 3 місяці тому

    nice explaination

  • @--Sreekarsai
    @--Sreekarsai 8 місяців тому +5

    why there is no code submision

  • @heartout9757
    @heartout9757 18 днів тому +1

    this fails for the case abba, so we can update to l=max(l,v[s[r]]+1)

  • @kodeeater
    @kodeeater 26 днів тому

    thankyou sir

  • @aggarwalsachin4854
    @aggarwalsachin4854 5 місяців тому

    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    int n = s.length(), l = 0, r = 0, cnt = 0;
    unordered_set seen;
    if (n == 0)
    return 0;
    while (r < n) {
    if (r < n && seen.find(s[r]) == seen.end()) {
    seen.insert(s[r]);
    cnt = max(cnt, r - l + 1);
    r++;
    } else {
    seen.erase(s[l]);
    l++;
    }
    }
    return cnt;
    }
    };

  • @bhuvangowda4842
    @bhuvangowda4842 3 місяці тому

    thank you bhaiii

  • @ok-jg9jb
    @ok-jg9jb 8 місяців тому +1

    Thanks❤

  •  4 місяці тому

    Thank you so much

  • @bijarnia_vikash
    @bijarnia_vikash 4 місяці тому

    Thank you for your explanation. it was nice.. I solved it.:)

  • @anshsaxena7297
    @anshsaxena7297 16 днів тому

    Understood.

  • @aditya_raj7827
    @aditya_raj7827 4 місяці тому

    Understood 😊😊

  • @DheerendraSingh-u2m
    @DheerendraSingh-u2m 2 місяці тому

    Understood❤❤❤

  • @shivisingh9975
    @shivisingh9975 7 місяців тому +1

    Understood!

  • @NonameNoname-f2t
    @NonameNoname-f2t 8 місяців тому +1

    Striverrrrrr❤❤❤❤❤🎉🎉

  • @shototodoroki4719
    @shototodoroki4719 8 місяців тому +1

    understood

  • @ashishpradhan6250
    @ashishpradhan6250 4 місяці тому

    Thanku

  • @messiisthebest
    @messiisthebest 7 місяців тому +1

    solution in python:
    class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
    hashmap={}
    left=0
    right=0
    Max=0
    while(right

  • @PawanKumar-hq6dy
    @PawanKumar-hq6dy 21 день тому

    can we also do it with set ? instead of hashmap. what issue would it cause

  • @The_Shubham_Soni
    @The_Shubham_Soni 4 місяці тому +1

    Test Case failed on GFG: input = qwertyuioplkjh
    output=13, correct output=14.

  • @AyushKumar-bi3qo
    @AyushKumar-bi3qo 2 місяці тому

    Can we do this longest substring without repeating characters problem using hashmap in Java?

  • @DSASolutions
    @DSASolutions 2 місяці тому

    Helpfull

  • @pardhi8959
    @pardhi8959 8 місяців тому

    Striver❤

  • @adityapandey23
    @adityapandey23 4 місяці тому

    Understood

  • @lrchoudhary6875
    @lrchoudhary6875 8 місяців тому +2

    int lengthOfLongestSubstring(string s) {
    int n=s.size();
    int ans=0;
    string a;
    for(int i=0;i

  • @mansisethi8127
    @mansisethi8127 6 місяців тому

    why do we need to do that range checking? I didn't understand

  • @viditmohil7102
    @viditmohil7102 7 місяців тому

    please upload string playlist

  • @utkarshsingh09
    @utkarshsingh09 Місяць тому

    Understood Sir!

  • @VaibhavSharma-f9g
    @VaibhavSharma-f9g 2 місяці тому

    can we use map here instead of hash array of 255 characters

  • @Praveen2002-e1k
    @Praveen2002-e1k 5 місяців тому

    hy can anyone explain why strivers use hash array instead of the data structure

  • @DJ-md3cj
    @DJ-md3cj 7 місяців тому

    great

  • @vanshgangwar3440
    @vanshgangwar3440 3 місяці тому

    Can we use map here ?

  • @VivekSharma-eh2tv
    @VivekSharma-eh2tv 6 місяців тому +2

    s= " " . why doesn't it clear this test case

  • @reshmijahaan1880
    @reshmijahaan1880 6 місяців тому

    where to find the language specific code

  • @minalRanjit
    @minalRanjit 7 місяців тому +2

    int lengthOfLongestSubstring(string s) {
    unordered_mapmpp;
    int ans=0,i=0;
    int n= s.size();
    int cnt = 0;
    if(s.size()==1){
    return 1;
    }
    while(i!=n){
    if(mpp[s[i]]==1){
    ans = max(ans,cnt);
    cnt=0;
    mpp.clear();
    }
    mpp[s[i]]++;
    cnt++;
    i++;
    }
    return ans;
    }
    //can anyone explain why it is failing the testcase of "au".

  • @bishalmahanta4398
    @bishalmahanta4398 5 місяців тому

    guys for input string having no repeating characters, i am getting wrong answer(one less than correct length) in leetcode
    for example input string="aus" ;
    the correct output is 3;
    but output from striver's optimal code is 2.Can someone help me out

    • @Anandsingh-bu5nq
      @Anandsingh-bu5nq 4 місяці тому

      its because you used array for hash use vector

    • @bishalmahanta4398
      @bishalmahanta4398 4 місяці тому +1

      @@Anandsingh-bu5nq thank you its working fine now;
      changed int hash[256]={-1}
      to vectorhash(256,-1)

    • @rajatrajgautam3224
      @rajatrajgautam3224 4 місяці тому

      @@bishalmahanta4398 what is the difference here between using vector and array?
      please elaborate

  • @UtkarshWasHereBeforeYou
    @UtkarshWasHereBeforeYou Місяць тому

    I did using binary search :(

  • @joychakraborty4367
    @joychakraborty4367 8 місяців тому +1

    ❤❤❤

  • @shaiksoofi3741
    @shaiksoofi3741 5 місяців тому

    undersood

  • @DollarSign124
    @DollarSign124 20 днів тому

    Fucking Art bro , gg

  • @subhasrisb11tha28
    @subhasrisb11tha28 2 місяці тому

  • @angeldeveloper
    @angeldeveloper 8 місяців тому

    🙌🏻

  • @sriramsivakumar5567
    @sriramsivakumar5567 5 місяців тому

    Give running code

  • @SAACHIPANDEY
    @SAACHIPANDEY 6 місяців тому

    10:44

  • @mind_blazed
    @mind_blazed 8 місяців тому

    Aise Q. Mere se nhi jamte basic jm jate 😢 plz koi batao how can i do ……

    • @obamaengineer4806
      @obamaengineer4806 8 місяців тому

      solve codeforces qs lots. ur programming and basic thinking will improve loads. solve prev contsets qs

    • @mind_blazed
      @mind_blazed 8 місяців тому

      @@obamaengineer4806 thank you I'll try to solve

    • @Cityscapes411
      @Cityscapes411 6 місяців тому

      Same situation mere sath thi kuch months pahle , but now I can solve even medium level questions easily , u just need practice, practice and more practice

    • @obamaengineer4806
      @obamaengineer4806 5 місяців тому

      @@Cityscapes411 WHICH PLATFORM U SOLVE FROM AND HOW MANY QS PER DAY AND ANY GENERAL TIPS?

  • @Manikandan__03
    @Manikandan__03 4 місяці тому

    Anybody please give that code for java

    • @maneesha5182
      @maneesha5182 4 місяці тому +1

      public int lengthOfLongestSubstring(String s) {
      HashSetmp=new HashSet();
      int i=0,j=0,max=0;
      while(j < s.length()){
      if(!mp.contains(s.charAt(j))){
      mp.add(s.charAt(j++));

      max=Math.max(max,j-i);
      }
      else
      {
      mp.remove(s.charAt(i++));
      }
      }
      return max;
      }

  • @AkashKumarTiwary-u4b
    @AkashKumarTiwary-u4b 6 місяців тому

    god

  • @rishabsharma5307
    @rishabsharma5307 4 місяці тому

    ```
    int lengthOfLongestSubstring(string s) {
    unordered_map mp;
    int i, j, n = s.size(), length = INT_MIN;
    i = j = 0;
    while(j < n)
    {
    mp[s[j]]++;
    if(mp[s[j]] == 1)
    length = max(length, j-i+1);
    while(mp[s[j]] > 1)
    {
    mp[s[i]]--;
    ++i;
    length = max(length, j-i+1);
    }
    ++j;
    }
    return length == INT_MIN ? 0 : length;
    }
    ```

  • @AdarshSingh-qd6mq
    @AdarshSingh-qd6mq 5 місяців тому

    👍

  • @prashantawasthi6825
    @prashantawasthi6825 3 місяці тому

    pata nhi ye kn log hai jinhe samjh aa raha hai. educated dikhne k lie english bole ja rahe jbki kisi ko bhi english me nahi samjh aata hoga India me

    • @onlyAI..
      @onlyAI.. Місяць тому

      Sab gawar nahi hai

  • @cenacr007
    @cenacr007 6 місяців тому

    us

  • @devil648
    @devil648 6 місяців тому

    Please anyone can help me.. Why we use hash[256] ={0};

    • @sprihaanand4128
      @sprihaanand4128 6 місяців тому

      initially all possible 256 character's frequency is 0

  • @DJ-md3cj
    @DJ-md3cj 7 місяців тому

    too long video

    • @NikhilSingh-ei2ft
      @NikhilSingh-ei2ft 6 місяців тому +1

      Beginners like me need such explanation. You also would have started from zero only

  • @hardikjain-brb
    @hardikjain-brb 7 місяців тому +2

    worst acche se code de doge to kya jayega bakwas explanation

  • @sushma_1704
    @sushma_1704 8 місяців тому

    Striver❤❤❤

  • @Shivi32590
    @Shivi32590 4 місяці тому

    understood

  • @SibiRanganathL
    @SibiRanganathL 3 місяці тому

    Understood

  • @Isagi__000
    @Isagi__000 8 місяців тому

    ❤❤

  • @anjalivas1111
    @anjalivas1111 Місяць тому

    understood