Javascript Program To Find Length Of The Longest Substring Without Repeating Characters
Last Updated :
17 Aug, 2023
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).
JavaScript
<script>
// JavaScript program to find the length of the
// longest substring without repeating
// characters
// This function returns true if all characters in
// str[i..j] are distinct, otherwise returns false
function areDistinct(str, i, j)
{
// Note : Default values in visited are false
var visited = new [26];
for(var 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.
function longestUniqueSubsttr(str)
{
var n = str.length();
// Result
var res = 0;
for(var i = 0; i < n; i++)
for(var j = i; j < n; j++)
if (areDistinct(str, i, j))
res = Math.max(res, j - i + 1);
return res;
}
// Driver code
var str = "geeksforgeeks";
document.write("The input string is " + str);
var len = longestUniqueSubsttr(str);
document.write("The length of the longest " +
"non-repeating character " +
"substring is " + len);
// This code is contributed by shivanisinghss2110.
</script>
OutputThe 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 repetition, we remove the previous occurrence and slide the window.
JavaScript
<script>
// JavaScript program to find the length of the
// longest substring without repeating
// characters
function longestUniqueSubsttr(str)
{
var n = str.length();
// Result
var res = 0;
for(var i = 0; i < n; i++)
{
// Note : Default values in visited are false
var visited = new [256];
for(var 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
var str = "geeksforgeeks";
document.write("The input string is " + str);
var len = longestUniqueSubsttr(str);
document.write("The length of the longest " +
"non-repeating character " +
"substring is " + len);
// This code is contributed by shivanisinghss2110
</script>
OutputThe input string is geeksforgeeks
The length of the longest non-repeating character substring is 7
Method 4 (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 :
JavaScript
<script>
// JavaScript program to find the length of the longest substring
// without repeating characters
var NO_OF_CHARS = 256;
function longestUniqueSubsttr(str)
{
var n = str.length();
var res = 0; // result
// last index of all characters is initialized
// as -1
var lastIndex = new [NO_OF_CHARS];
Arrays.fill(lastIndex, -1);
// Initialize start of current window
var i = 0;
// Move end of current window
for (var 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 */
var str = "geeksforgeeks";
document.write("The input string is " + str);
var len = longestUniqueSubsttr(str);
document.write("The length of the longest non repeating character is " + len);
// This code is contributed by shivanisinghss2110
</script>
OutputThe 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)
Please refer complete article on
Length of the longest substring without repeating characters for more details!
Similar Reads
Longest Substring Without Repeating Characters 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: 1E
12 min read
Longest Even Length Substring such that Sum of First and Second Half is same Given a string 'str' of digits, find the length of the longest substring of 'str', such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits. Examples : Input: str = "123123" Output: 6 The complete string is of even length and sum of first and
15+ min read
Longest substring with k unique characters Given a string s and a non negative integer k, find the length of the longest substring that contains exactly k distinct characters. If no such substring exists, return -1.Examples: Input: s = "aabacbebebe", k = 3Output: 7Explanation: The longest substring with exactly 3 distinct characters is "cbeb
15+ min read
Longest substring with k unique characters Given a string s and a non negative integer k, find the length of the longest substring that contains exactly k distinct characters. If no such substring exists, return -1.Examples: Input: s = "aabacbebebe", k = 3Output: 7Explanation: The longest substring with exactly 3 distinct characters is "cbeb
15+ min read
Length of the smallest sub-string consisting of maximum distinct characters Given a string of length N, find the length of the smallest sub-string consisting of maximum distinct characters. Note : Our output can have same character. Examples: Input : "AABBBCBB"Output : 5Input : "AABBBCBBAC"Output : 3Explanation : Sub-string -> "BAC"Input : "GEEKSGEEKSFOR"Output : 8Explan
15+ min read
Smallest window that contains all characters of string itself Given a string str, your task is to find the smallest window length that contains all the characters of the given string at least one time.Examples: Input: str = "aabcbcdbca"Output: 4Explanation: Sub-string -> "dbca"Input: str = "aaab"Output: 2Explanation: Sub-string -> "ab"Table of Content[Na
8 min read