VOOZH about

URL: https://www.geeksforgeeks.org/dsa/length-of-the-longest-substring-without-repeating-characters/

⇱ Longest Substring Without Repeating Characters - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Longest Substring Without Repeating Characters

Last Updated : 20 Jan, 2026

Given a string s having lowercase characters, find the length of the longest substring without repeating characters. 

Examples:

Input: s = "geeksforgeeks"
Output: 7
Explanation: The longest substrings without repeating characters are "eksforg” and "ksforge", with lengths of 7.

Input: s = "aaa"
Output: 1
Explanation: The longest substring without repeating characters is "a"

Input: s = "abcdefabcbb"
Output: 6
Explanation: The longest substring without repeating characters is "abcdef".

[Naive Approach] Substrings Starting From Every Index - O(n^2) Time and O(1) Space

The idea is to find length of longest substring with distinct characters starting from every index and maximum of all such lengths will be our answer.

To find the length of the longest substring with distinct characters starting from an index, we create a new visited array of size = 26 to keep track of included characters in the substring. vis[0] checks for 'a', vis[1] checks for 'b', vis[2] checks for 'c' and so on.

Note: The array size is fixed at 26, representing the lowercase English alphabet as a constant


Output
7

Time Complexity: O(n2), The outer loop runs O(n) times and the inner loop may also run up to O(n) in the worst case. The visited array initialization costs O(26), which is constant and does not affect overall complexity.
Auxiliary Space: O(1), vis array has size 26 which is constant.

[Expected Approach 1] Using Sliding Window - O(n) Time and O(1) Space

The idea is to maintain a window of distinct characters. The window is initialized as single character. We keep extending the window on the right side till we see distinct characters. When we see a repeating character, we remove characters from the left side of the window. We keep track of the maximum length window.

Below are the detailed steps:

  • Initialize two pointers left and right with 0, which define the current window being considered.
  • The right pointer moves from left to right, extending the current window.
  • If the character at right pointer is not visited, it's marked as visited.
  • If the character at right pointer is visited, it means there is a repeating character. The left pointer moves to the right while marking visited characters as false until the repeating character is no longer part of the current window.
  • The length of the current window (right - left + 1) is calculated and answer is updated accordingly.

Working:


Output
7

[Expected Approach 2] Using Last Index of Each Character - O(n) Time and O(1) Space

The approach stores the last indexes of already visited characters. The idea is to maintain a window of distinct characters. Start from the first character, and keep extending the window on the right side till we see distinct characters. When we see a repeating character, we check for the last index of the repeated character:

  • If last index of repeated character >= starting index of the current window, then we update the starting index of the current window to last index of repeated character + 1 to remove the repeated character.
  • If last index of repeated character <starting index of the current window, then it means that the repeated character is already outside the current window so the window size remains unchanged.

After iterating over all the characters, the largest window size will be our answer.

Working:


Output
7


Comment