Skip to content
Related Articles

Related Articles

Length of the longest substring without repeating characters
  • Difficulty Level : Medium
  • Last Updated : 19 Feb, 2021
 

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

  • For “ABDEFGABEF”, the longest substring are “BDEFGA” and “DEFGAB”, with length 6.
  • For “BBBB” the longest substring is “B”, with length 1.
  • For “GEEKSFORGEEKS”, there are two longest substrings shown in the below diagrams, with length 7

 

The desired time complexity is O(n) where n is the length of the string.
 



 

Method 1 (Simple : O(n3)): We can consider all substrings one by one and check for each substring whether it contains all unique characters or not. There will be n*(n+1)/2 substrings. Whether a substring contains all unique characters or not can be checked in linear time by scanning it from left to right and keeping a map of visited characters. Time complexity of this solution would be O(n^3).

C++




// C++ program to find the length of the longest substring
// without repeating characters
#include <bits/stdc++.h>
using namespace std;
 
// This functionr eturns true if all characters in str[i..j]
// are distinct, otherwise returns false
bool areDistinct(string str, int i, int j)
{
 
    // Note : Default values in visited are false
    vector<bool> visited(26);
 
    for (int k = i; k <= j; k++) {
        if (visited[str[k] - 'a'] == true)
            return false;
        visited[str[k] - 'a'] = true;
    }
    return true;
}
 
// Returns length of the longest substring
// with all distinct characters.
int longestUniqueSubsttr(string str)
{
    int n = str.size();
    int res = 0; // result
    for (int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
            if (areDistinct(str, i, j))
                res = max(res, j - i + 1);
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << "The input string is " << str << endl;
    int len = longestUniqueSubsttr(str);
    cout << "The length of the longest non-repeating "
            "character substring is "
         << len;
    return 0;
}


Java




// Java program to find the length of the
// longest substring without repeating
// characters
import java.io.*;
import java.util.*;
 
class GFG{
 
// This function returns true if all characters in
// str[i..j] are distinct, otherwise returns false
public static Boolean areDistinct(String str,
                                  int i, int j)
{
     
    // Note : Default values in visited are false
    boolean[] visited = new boolean[26];
 
    for(int k = i; k <= j; k++)
    {
        if (visited[str.charAt(k) - 'a'] == true)
            return false;
             
        visited[str.charAt(k) - 'a'] = true;
    }
    return true;
}
 
// Returns length of the longest substring
// with all distinct characters.
public static int longestUniqueSubsttr(String str)
{
    int n = str.length();
     
    // Result
    int res = 0;
     
    for(int i = 0; i < n; i++)
        for(int j = i; j < n; j++)
            if (areDistinct(str, i, j))
                res = Math.max(res, j - i + 1);
                 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.println("The input string is " + str);
 
    int len = longestUniqueSubsttr(str);
     
    System.out.println("The length of the longest " +
                       "non-repeating character " +
                       "substring is " + len);
}
}
 
// This code is contributed by akhilsaini


Python3




# Python3 program to find the length
# of the longest substrring without
# repeating characters
 
# This functionr eturns true if all
# characters in strr[i..j] are
# distinct, otherwise returns false
def areDistinct(strr, i, j):
 
    # Note : Default values in visited are false
    visited = [0] * (26)
 
    for k in range(i, j + 1):
        if (visited[ord(strr[k]) -
                   ord('a')] == True):
            return False
             
        visited[ord(strr[k]) -
                ord('a')] = True
 
    return True
 
# Returns length of the longest substrring
# with all distinct characters.
def longestUniqueSubsttr(strr):
     
    n = len(strr)
     
    # Result
    res = 0
     
    for i in range(n):
        for j in range(i, n):
            if (areDistinct(strr, i, j)):
                res = max(res, j - i + 1)
                 
    return res
 
# Driver code
if __name__ == '__main__':
     
    strr = "geeksforgeeks"
    print("The input is ", strr)
     
    len = longestUniqueSubsttr(strr)
    print("The length of the longest "
          "non-repeating character substring is ", len)
 
# This code is contributed by mohit kumar 29


C#




// C# program to find the length of the
// longest substring without repeating
// characters
using System;
  
class GFG{
  
// This function returns true if all characters in
// str[i..j] are distinct, otherwise returns false
public static bool areDistinct(string str,
                               int i, int j)
{
     
    // Note : Default values in visited are false
    bool[] visited = new bool[26];
  
    for(int k = i; k <= j; k++)
    {
        if (visited[str[k] - 'a'] == true)
            return false;
              
        visited[str[k] - 'a'] = true;
    }
    return true;
}
  
// Returns length of the longest substring
// with all distinct characters.
public static int longestUniqueSubsttr(string str)
{
    int n = str.Length;
      
    // Result
    int res = 0;
      
    for(int i = 0; i < n; i++)
        for(int j = i; j < n; j++)
            if (areDistinct(str, i, j))
                res = Math.Max(res, j - i + 1);
                  
    return res;
}
  
// Driver code
public static void Main()
{
    string str = "geeksforgeeks";
    Console.WriteLine("The input string is " + str);
  
    int len = longestUniqueSubsttr(str);
      
    Console.WriteLine("The length of the longest " +
                      "non-repeating character " +
                      "substring is " + len);
}
}
 
// This code is contributed by sanjoy_62


 
 

Output: 

The input string is geeksforgeeks
The length of the longest non-repeating character substring is 7

 

Method 2 (Better : O(n2)) The idea is to use window sliding. Whenever we see repitition, we remove the pervious occurrance and slide the window.

 

C++




// C++ program to find the length of the longest substring
// without repeating characters
#include <bits/stdc++.h>
using namespace std;
 
int longestUniqueSubsttr(string str)
{
    int n = str.size();
    int res = 0; // result
 
    for (int i = 0; i < n; i++) {
         
        // Note : Default values in visited are false
        vector<bool> visited(256);  
 
        for (int j = i; j < n; j++) {
 
            // If current character is visited
            // Break the loop
            if (visited[str[j]] == true)
                break;
 
            // Else update the result if
            // this window is larger, and mark
            // current character as visited.
            else {
                res = max(res, j - i + 1);
                visited[str[j]] = true;
            }
        }
 
        // Remove the first character of previous
        // window
        visited[str[i]] = false;
    }
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << "The input string is " << str << endl;
    int len = longestUniqueSubsttr(str);
    cout << "The length of the longest non-repeating "
            "character substring is "
         << len;
    return 0;
}


Java




// Java program to find the length of the
// longest substring without repeating
// characters
import java.io.*;
import java.util.*;
 
class GFG{
 
public static int longestUniqueSubsttr(String str)
{
    int n = str.length();
     
    // Result
    int res = 0;
     
    for(int i = 0; i < n; i++)
    {
         
        // Note : Default values in visited are false
        boolean[] visited = new boolean[256];
         
        for(int j = i; j < n; j++)
        {
             
            // If current character is visited
            // Break the loop
            if (visited[str.charAt(j)] == true)
                break;
 
            // Else update the result if
            // this window is larger, and mark
            // current character as visited.
            else
            {
                res = Math.max(res, j - i + 1);
                visited[str.charAt(j)] = true;
            }
        }
 
        // Remove the first character of previous
        // window
        visited[str.charAt(i)] = false;
    }
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    System.out.println("The input string is " + str);
 
    int len = longestUniqueSubsttr(str);
    System.out.println("The length of the longest " +
                       "non-repeating character " +
                       "substring is " + len);
}
}
 
// This code is contributed by akhilsaini


Python3




# Python3 program to find the
# length of the longest substring
# without repeating characters
def longestUniqueSubsttr(str):
     
    n = len(str)
     
    # Result
    res = 0
  
    for i in range(n):
          
        # Note : Default values in
        # visited are false
        visited = [0] * 256  
  
        for j in range(i, n):
  
            # If current character is visited
            # Break the loop
            if (visited[ord(str[j])] == True):
                break
  
            # Else update the result if
            # this window is larger, and mark
            # current character as visited.
            else:
                res = max(res, j - i + 1)
                visited[ord(str[j])] = True
             
        # Remove the first character of previous
        # window
        visited[ord(str[i])] = False
     
    return res
 
# Driver code
str = "geeksforgeeks"
print("The input is ", str)
 
len = longestUniqueSubsttr(str)
print("The length of the longest "
      "non-repeating character substring is ", len)
 
# This code is contributed by sanjoy_62


C#




// C# program to find the length of the
// longest substring without repeating
// characters
using System;
 
class GFG{
     
static int longestUniqueSubsttr(string str)
{
    int n = str.Length;
     
    // Result
    int res = 0;
   
    for(int i = 0; i < n; i++)
    {
         
        // Note : Default values in visited are false
        bool[] visited = new bool[256];
         
        // visited = visited.Select(i => false).ToArray();
        for(int j = i; j < n; j++)
        {
             
            // If current character is visited
            // Break the loop
            if (visited[str[j]] == true)
                break;
   
            // Else update the result if
            // this window is larger, and mark
            // current character as visited.
            else
            {
                res = Math.Max(res, j - i + 1);
                visited[str[j]] = true;
            }
        }
         
        // Remove the first character of previous
        // window
        visited[str[i]] = false;
    }
    return res;
}
 
// Driver code
static void Main()
{
    string str = "geeksforgeeks";
    Console.WriteLine("The input string is " + str);
     
    int len = longestUniqueSubsttr(str);
    Console.WriteLine("The length of the longest " +
                      "non-repeating character " +
                      "substring is " + len );
}
}
 
// This code is contributed by divyeshrabadiya07


 
 

Output: 



The input string is geeksforgeeks
The length of the longest non-repeating character substring is 7

 

Method 3 (Linear Time): Let us talk about the linear time solution now. This solution uses extra space to store the last indexes of already visited characters. The idea is to scan the string from left to right, keep track of the maximum length Non-Repeating Character Substring seen so far in res. When we traverse the string, to know the length of current window we need two indexes. 
1) Ending index ( j ) : We consider current index as ending index. 
2) Starting index ( i ) : It is same as previous window if current character was not present in the previous window. To check if the current character was present in the previous window or not, we store last index of every character in an array lasIndex[]. If lastIndex[str[j]] + 1 is more than previous start, then we updated the start index i. Else we keep same i.  

Below is the implementation of the above approach :

 

C++




// C++ program to find the length of the longest substring
// without repeating characters
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
 
int longestUniqueSubsttr(string str)
{
    int n = str.size();
 
    int res = 0; // result
 
    // last index of all characters is initialized
    // as -1
    vector<int> lastIndex(NO_OF_CHARS, -1);
 
    // Initialize start of current window
    int i = 0;
 
    // Move end of current window
    for (int j = 0; j < n; j++) {
 
        // Find the last index of str[j]
        // Update i (starting index of current window)
        // as maximum of current value of i and last
        // index plus 1
        i = max(i, lastIndex[str[j]] + 1);
 
        // Update result if we get a larger window
        res = max(res, j - i + 1);
 
        // Update last index of j.
        lastIndex[str[j]] = j;
    }
    return res;
}
 
// Driver code
int main()
{
    string str = "geeksforgeeks";
    cout << "The input string is " << str << endl;
    int len = longestUniqueSubsttr(str);
    cout << "The length of the longest non-repeating "
            "character substring is "
         << len;
    return 0;
}


Java




// Java program to find the length of the longest substring
// without repeating characters
import java.util.*;
 
public class GFG {
 
    static final int NO_OF_CHARS = 256;
 
    static int longestUniqueSubsttr(String str)
    {
        int n = str.length();
 
        int res = 0; // result
 
        // last index of all characters is initialized
        // as -1
        int [] lastIndex = new int[NO_OF_CHARS];
        Arrays.fill(lastIndex, -1);
 
        // Initialize start of current window
        int i = 0;
 
        // Move end of current window
        for (int j = 0; j < n; j++) {
 
            // Find the last index of str[j]
            // Update i (starting index of current window)
            // as maximum of current value of i and last
            // index plus 1
            i = Math.max(i, lastIndex[str.charAt(j)] + 1);
 
            // Update result if we get a larger window
            res = Math.max(res, j - i + 1);
 
            // Update last index of j.
            lastIndex[str.charAt(j)] = j;
        }
        return res;
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        System.out.println("The input string is " + str);
        int len = longestUniqueSubsttr(str);
        System.out.println("The length of "
                           + "the longest non repeating character is " + len);
    }
}
// This code is contributed by Sumit Ghosh


Python3




# Python3 program to find the length of the longest substring
# without repeating characters
NO_OF_CHARS = 256
 
def longestUniqueSubsttr(string):
 
    # Initialize the last index array as -1, -1 is used to store
    # last index of every character
    lastIndex = [-1] * NO_OF_CHARS
 
    n = len(string)
    res = 0   # Result
    i = 0
 
    for j in range(0, n):
        # Find the last index of str[j]
        # Update i (starting index of current window)
        # as maximum of current value of i and last
        # index plus 1
        i = max(i, lastIndex[ord(string[j])] + 1);
 
        # Update result if we get a larger window
        res =  max(res, j - i + 1)
 
        # Update last index of j.
        lastIndex[ord(string[j])] = j;
 
    return res
 
# Driver program to test the above function
string = "geeksforgeeks"
print ("The input string is " + string)
length = longestUniqueSubsttr(string)
print ("The length of the longest non-repeating character" +
       " substring is " + str(length))
 
# This code is contributed by Bhavya Jain


C#




// C# program to find the length of the longest substring
// without repeating characters
using System;
public class GFG
{
  static int NO_OF_CHARS = 256;
  static int longestUniqueSubsttr(string str)
  {
    int n = str.Length;
    int res = 0; // result
 
    // last index of all characters is initialized
    // as -1
    int [] lastIndex = new int[NO_OF_CHARS];
    Array.Fill(lastIndex, -1);
 
    // Initialize start of current window
    int i = 0;
 
    // Move end of current window
    for (int j = 0; j < n; j++)
    {
 
      // Find the last index of str[j]
      // Update i (starting index of current window)
      // as maximum of current value of i and last
      // index plus 1
      i = Math.Max(i, lastIndex[str[j]] + 1);
 
      // Update result if we get a larger window
      res = Math.Max(res, j - i + 1);
 
      // Update last index of j.
      lastIndex[str[j]] = j;
    }
    return res;
  }
 
  /* Driver program to test above function */
  static public void Main ()
  {
    string str = "geeksforgeeks";
    Console.WriteLine("The input string is " + str);
    int len = longestUniqueSubsttr(str);
    Console.WriteLine("The length of "+
                      "the longest non repeating character is " +
                      len);
  }
}
 
// This code is contributed by avanitrachhadiya2155


Output: 

The input string is geeksforgeeks
The length of the longest non-repeating character substring is 7

Time Complexity: O(n + d) where n is length of the input string and d is number of characters in input string alphabet. For example, if string consists of lowercase English characters then value of d is 26. 
Auxiliary Space: O(d) 

Alternate Implementation : 

Python




# Here, we are planning to implement a simple sliding window methodology
  
def longestUniqueSubsttr(string):
      
    # Creating a set to store the last positions of occurrence
    seen = {}
    maximum_length = 0
  
    # starting the inital point of window to index 0
    start = 0
      
    for end in range(len(string)):
  
        # Checking if we have already seen the element or not
        if string[end] in seen:
 
            # If we have seen the number, move the start pointer
            # to position after the last occurrence
            start = max(start, seen[string[end]] + 1)
  
        # Updating the last seen value of the character
        seen[string[end]] = end
        maximum_length = max(maximum_length, end-start + 1)
    return maximum_length
  
# Driver Code
string = "geeksforgeeks"
print("The input string is", string)
length = longestUniqueSubsttr(string)
print("The length of the longest non-repeating character substring is", length)


Output: 

('The input string is', 'geeksforgeeks')
('The length of the longest non-repeating character substring is', 7)

As an exercise, try the modified version of the above problem where you need to print the maximum length NRCS also (the above program only prints the length of it).

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :