Length of the longest substring | Practice | GeeksforGeeks

Given a string S, find the length of the longest substring without repeating characters. 

Example 1:
Input:
S = "geeksforgeeks"
Output:
7
Explanation:
Longest substring is
"eksforg".
Example 2:
Input:
S = "abdefgabef"
Output:
6
Explanation:
Longest substring are
"abdefg" , "bdefga" and "defgab".
 
Your Task:
You don't need to take input or print anything. Your task is to complete the function longestUniqueSubsttr() which takes a string S as and returns the length of the longest substring. 
 
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(K) where K is constant.
0
himanshukug19cs21 hours ago
  int ans=0;     ArrayDeque<Character> dq = new ArrayDeque<>();     for(int i=0;i<S.length();i++){         char ch=S.charAt(i);         if(!dq.contains(ch)){             dq.add(ch);         }         else{             ans=Math.max(ans,dq.size());             while(dq.contains(ch))dq.remove();             dq.add(ch);     }     }     if(!dq.isEmpty())         ans=Math.max(ans,dq.size());     return ans;
+1
badgujarsachin833 days ago
int longestUniqueSubsttr(string S){
       //code
       int maxi=INT_MIN, i=0,j=0,n=S.size();
       int arr[26]={0};
       while(j<n){
          arr[S[j]-'a']++;
          while(i<n && arr[S[j]-'a']!=1){
              arr[S[i]-'a']--;
              i++;
          }
          maxi=max(maxi,j-i+1);
          j++;
       }
       return maxi;
    }
0
kake13372 weeks ago
    int longestUniqueSubsttr(string S){
       vector<int>v(26,-1);
       int i=0,j=0,r=1;
       while(j<S.length())
       {
           if(v[S[j]-'a']!=-1)
           {
               if(v[S[j]-'a']>=i)
               i=v[S[j]-'a']+1;
           }
           v[S[j]-'a']=j;
           r=max(r,j-i+1);
           j++;
       }
       return r;
   }
0
koresachin19993 weeks ago
class Solution{   int longestUniqueSubsttr(String s){       int n=s.length();              HashMap<Character, Integer> map=new HashMap<>();       int ans=0;       int start=0, end=0;       while(end<n)       {           map.put(s.charAt(end), map.getOrDefault(s.charAt(end),0)+1);                      while(start<=end&& map.get(s.charAt(end))>1)           {             map.put(s.charAt(start),map.get(s.charAt(start))-1);             start++;           }           ans=Math.max(ans, end-start+1);           end++;       }       return ans;   }}
0
kumaraditya338713 weeks ago
// please up vote this code  ☺️
 
 
 int longestUniqueSubsttr(string S){       //code
      int i=0;      int j=0;      int mx=INT_MIN;      map<char,int>mp;      while(j<S.length())      {          mp[S[j]]++;        if(mp.size()>j-i+1)          j++;     else  if(mp.size()==j-i+1)          {              mx=max(mx ,  j-i+1);              j++;          }         else if(mp.size()<j-i+1)          {              while(mp.size()<j-i+1)              {                  mp[S[i]]-- ;                  if(mp[S[i]]==0)                  mp.erase(S[i]);                  i++;              }                    j++;     } 
}//while loop end   return mx;          }
 
0
ujjwalraj57983 weeks ago
O(N) solution using two pointers
int longestUniqueSubsttr(string S){
        //code
        int n=S.length();
        vector<int>freq;
        freq.resize(26,0);
        int ans=0;
        int head=-1;int tail=0;
        while(tail<n){
            //head move
            while(head+1<n&&freq[S[head+1]-'a']==0){
                head++;
                freq[S[head]-'a']++;
            }
            //process
            ans=max(ans,head-tail+1);
            //tail updation
            if(head>=tail){
                freq[S[tail]-'a']--;
                tail++;
            }else{
                tail++;
                head=tail-1;
            }
            
        }
        return ans;
    }
0
iamkhijiralam1 month ago
Simple C++ | O(N) solution |Accepted
 
int longestUniqueSubsttr(string S){       //code       int start=0,next=0;       vector<int> lastIndex(256,-1);       int longestSoFar=0;       while(S[next]!='\0'){           char nextChar=S[next];           if(lastIndex[nextChar]<start){               next++;           }           else{                 int currentOP=next-start;                if(currentOP > longestSoFar){                  longestSoFar= currentOP;                  }               start=lastIndex[nextChar] +1;               next++;           }           lastIndex[nextChar]=next-1;                  }       int currentOP=next-start;       if(currentOP > longestSoFar){            longestSoFar= currentOP;            }       return longestSoFar;          }
+1
namantam11 month ago
Simple C++ | O(N) solution | Without set or hashmap
 
class Solution{
    int arr[26] = {0};
    
    bool contains(char c) {
        return arr[c - 'a'];
    }
    
    void erase(char c) {
        arr[c - 'a'] = 0;
    }
    
    void insert(char c) {
        arr[c - 'a'] = 1;
    }
    
    public:
    int longestUniqueSubsttr(string s){
        int i=0, j=0, ans=0, n=s.size();
        
        while (j < n) {
            while (i<n and contains(s[j])) {
                erase(s[i++]);
            }
            insert(s[j++]);
            
            ans = max(ans, j-i);
        }
        
        return ans;
    }
};
+1
abhinavjain78981 month ago
Java Easy Solution Using HashMap
class Solution{
    int longestUniqueSubsttr(String s){
        HashMap<Character,Integer> hm = new HashMap<>();
        int max = 0;
        int n = s.length();
        for(int i=0;i<n;i++){
            char temp = s.charAt(i);
            if(!hm.containsKey(temp)){
                hm.put(temp,i);
            }
            else{
                i = hm.get(temp);
                hm.clear();
            }
            if(hm.size()>max){
            max = hm.size();
            }
        }
        return max;
    }
}
0
mebhowmik3211 month ago
Successful C++ solution:
int longestUniqueSubsttr(string S){
        //code
        vector<int> vec(26,0);
        int res=0;
        int l=0,r=0,n=S.length();
        while(r<n)
        {
            int c=S[r]-'a';
            vec[c]++;
            while(vec[c]>1)
                vec[S[l++]-'a']--;
            res=max(r-l+1,res);
            r++;
        }
        return res;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.