Longest Substring Without Repeating Characters | Leetcode 3 | Sliding window

Sdílet
Vložit
  • čas přidán 6. 09. 2024
  • Time Complexity : O(n)
    Space Complexity : O(1)
    Problem Link : leetcode.com/p...
    C++ Code Link : github.com/Ayu...
    Please like, share and subscribe if you found the video useful. Feel free to ask in comments section if you have any doubts. :)
    #DataStructuresAndAlgorithms
    #Leetcode
    #interviewpreparation
    #AyushiSharma
    Longest Substring Without Repeating Characters solution
    Longest Substring Without Repeating Characters Leetcode
    Longest Substring Without Repeating Characters C++
    Longest Substring Without Repeating Characters Java
    Longest Substring Without Repeating Characters Python
    🔥🔥🔥🔥👇👇👇
    Join telegram channel for more updates on placement preparation : t.me/AyushiSha...
    Checkout the series: 🔥🔥🔥
    👉Interview Experiences : • Interview Experiences
    👉 Array: • Arrays
    👉 Linked List : • Linked List
    👉 Heap : • Heap
    👉 Recursion : • Recursion
    👉 Stack and Queue : • Stack And Queue
    👉 Greedy : • Greedy
    👉 Dynamic Programming : • Dynamic Programming
    👉 Leetcode contests : • Leetcode contests
    👉 Leetcode June Challenge : • Leetcode June Challenge
    👉 Leetcode July Challenge : • Leetcode July Challenge
    LIKE | SHARE | SUBSCRIBE 🔥🔥😊

Komentáře • 75

  • @jasper5016
    @jasper5016 Před 11 měsíci +3

    Thanks. Here is the Java code if someone needs it -
    public int maxSubString(String s) {
    int[] count = new int[256];
    int l = 0;
    int r = 0;
    int ans = 0;
    while (r < s.length()) {
    char currChar = s.charAt(r);
    count[currChar]++;
    while (count[currChar] > 1) {
    count[s.charAt(l)]--;
    l++;
    }
    ans = Math.max(ans, r - l + 1);
    r++;
    }
    return ans;
    }

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před 2 měsíci +1

      thank you for sharing your code and helping others :)

  • @azhargayawi
    @azhargayawi Před 4 měsíci +2

    after wasting approx 5 hours on you tube i found the genuine logic here, thank you mam. greaat....

  • @cs_soldier5292
    @cs_soldier5292 Před rokem +3

    This can help someone :)
    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {
    //creating a set to store the list
    unordered_set ans;
    int value=0;
    int i=0,j=0;
    while(i

  • @coding619
    @coding619 Před 10 měsíci +1

    Good explanation but I guess the space complexity mentioned in the description should be O(k), where k are the unique characters and not O(1).

  • @FootB_shorts1
    @FootB_shorts1 Před měsícem

    After wasting a lot of time i found A helpful video .
    thanks❤❤

  • @venkatvasamsetti8125
    @venkatvasamsetti8125 Před 2 lety +1

    I have already done this problem using hashmap.but without sliding window.thank you for this optimization of code

  • @user-er4hz8dg1g
    @user-er4hz8dg1g Před 6 měsíci

    hi aushi, using two while loop may increase time complexity else we can use hash map for storing index of characters

  • @ashjournal
    @ashjournal Před 2 lety +3

    Waiting for Chennai vlog part 2 😍

  • @luckilygamer5462
    @luckilygamer5462 Před rokem

    int lengthOfLongestSubstring(string s)
    {
    unordered_map umap;
    int n = s.length();
    int ans=0;
    int start=0;
    for (int i = 0; i < n; i++) {
    if(umap.find(s[i])!=umap.end()){
    // update the start index to the next character after the last occurrence
    start = max(start, umap[s[i]] + 1);
    }
    umap[s[i]] = i;
    ans=max(ans,i-start+1);
    }
    return ans;
    }

  • @priyankakataria7922
    @priyankakataria7922 Před 2 lety

    Time complexity worst case will be O(n^2) right? Coz two while loops

  • @ankitris5201
    @ankitris5201 Před rokem +1

    crystal clear explanation mam 👍👍

  • @BADASSBLADE
    @BADASSBLADE Před 2 lety

    Solve Today's Leetcode Challenge(Erasure Problem,) by using this approach, thankyou keep up the good work

  • @harshvardhan7762
    @harshvardhan7762 Před rokem

    for java coders, there will be slight changes:-
    public int lengthOfLongestSubstring(String s) {
    int[] arr = new int[128];
    int n = s.length();
    int len = 0;
    for(int i = 0, j = 0; j < n; j++){
    int c = s.charAt(j);
    arr[c]++;
    if(arr[c] > 1){
    while(arr[c] > 1){
    arr[s.charAt(i)]--;
    i++;
    }
    }
    len = Math.max(len, j - i + 1);
    }
    return len;
    }

  • @SAHIL-jm2hv
    @SAHIL-jm2hv Před 24 dny

    Thanks ma'am

  • @abhishekprasad7568
    @abhishekprasad7568 Před 2 lety +1

    Di should i buy coding ninja dsa course or study from free resource . pls response

    • @AyushiSharmaDSA
      @AyushiSharmaDSA  Před 2 lety

      The course is good and you can learn from free resources also, so it totally depends on you :)

  • @pratiksuman007
    @pratiksuman007 Před 9 měsíci

    ​ @AyushiSharmaDSA Your solution seems better then what available in general platform. What resource you have used to train yourself, could u make a video of it or if already there then link pls?

  • @Srihan_kumar
    @Srihan_kumar Před rokem

    In gfg this code is not working

  • @vamsiv5767
    @vamsiv5767 Před 17 dny

    This code gives wrong answer for aabccbb

  • @jaikrishnapandey632
    @jaikrishnapandey632 Před 2 lety +1

    Great explanation

  • @jagmohangautam1062
    @jagmohangautam1062 Před 6 měsíci

    it is giving runtime error for string containing space

  • @abhisheknavhal1039
    @abhisheknavhal1039 Před rokem

    The solution for the test case 'pwwkew' is not giving the correct answer.

  • @samyakagarwal1777
    @samyakagarwal1777 Před 10 měsíci

    very well explained ...........100/100

  • @vinaykulkarni8863
    @vinaykulkarni8863 Před 7 měsíci

    int lengthOfLongestSubstring(string s) {
    unordered_mapmp;
    int i=0,j=0,ans=0;
    int n=s.length();
    while(j1){
    mp[s[i]]--;
    i++;
    }
    ans=max(ans,j-i+1);
    j++;
    }
    return ans;
    }
    };

  • @aman_kumar0208
    @aman_kumar0208 Před 2 lety +1

    Thanks di

  • @bellanatrisha1201
    @bellanatrisha1201 Před rokem

    how is taking count[a] ?

  • @kishorjanjal7454
    @kishorjanjal7454 Před 2 lety

    Thank for crisp solution 😌😌

  • @harishsonawane855
    @harishsonawane855 Před 2 lety +1

    Nice video

  • @akashonlinehere
    @akashonlinehere Před rokem

    that code gives wrong ans in leetcode

  • @softwareengineering101

    Nice explanation

  • @arqamazmi3327
    @arqamazmi3327 Před 2 lety

    Nice Explanation
    Try this also.
    Longest Repeating Character Replacement

  • @ShaliniSingh-yo4rt
    @ShaliniSingh-yo4rt Před 2 lety

    Can we take char array of 128 size??

  • @knixkcodes
    @knixkcodes Před rokem

    very well explained!

  • @ashjournal
    @ashjournal Před 2 lety +1

    👍👍

  • @sumitpal9113
    @sumitpal9113 Před 2 lety

    Java code is not working in testcase "pwwkew". can any one help me.
    public int lengthOfLongestSubstring(String s) {

    int count[] = new int[256];

    int maxLen = 0, i=0, j=0;

    while(j < s.length()){
    int index = s.charAt(j);
    count[index]++;

    while(count[index] > 1){
    count[index]--;
    i++;
    }

    maxLen = Math.max(maxLen,( j-i)+1);

    j++;
    }

    return maxLen;
    }

    • @mohammedafreed9741
      @mohammedafreed9741 Před 2 lety

      In second while loop it's count[s.charAt(i)] not index. And why are you taking extra variable index

    • @abhisheknavhal1039
      @abhisheknavhal1039 Před rokem

      This solution is not working for pwwkew case

    • @harshvardhan7762
      @harshvardhan7762 Před rokem

      @@abhisheknavhal1039 in line 8, it will be ************** count[s.charAt(i)]--; ******************************will be there

  • @bibs24
    @bibs24 Před 2 lety

    Thanks a ton!

  • @dhanrajlouvanshi7872
    @dhanrajlouvanshi7872 Před 2 lety

    i am waiting for this

  • @deeptimayeemaharana2448

    You're best😁

  • @amitsinha806
    @amitsinha806 Před 2 lety +1

    👍👍👍👍👍👍👍👍👍👍👍👍👍👍

  • @UdayGarg
    @UdayGarg Před rokem

    thanks
    ]

  • @NikhilKumar-br4nj
    @NikhilKumar-br4nj Před 2 lety

    Didi if possible pls give java code also

  • @user-uu2zk1tj6e
    @user-uu2zk1tj6e Před 6 měsíci

    You could've just took i and j. Why did you made it look difficult. We are beginners that's why we are here watching your videos. Please take care of it next time. Nice Explanation tho

  • @CoDestination
    @CoDestination Před 7 měsíci

    Hindi Java
    czcams.com/video/hG2Fg6FlRA0/video.htmlsi=DlUeAE29pJsP5viX

  • @umang1226
    @umang1226 Před 2 lety

    please make in hindi we are more comfortable in it .

  • @restless4263
    @restless4263 Před 2 lety

    nice explanation

  • @SuryajyotiDas
    @SuryajyotiDas Před rokem

    thanks ma'am

  • @KunalKashyap6
    @KunalKashyap6 Před 2 lety

    thanku di