-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-nonrepeating-substring.js
38 lines (32 loc) · 1.43 KB
/
longest-nonrepeating-substring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Given a string str, find the length of the longest substring without repeating characters.
*
* @param {*} str
*/
function longestNonrepeatingSubstring(str) {
let sLongestNonrepeatingSubstring = "";
let sPossibleLongestNonrepeatingSubstring = "";
let sCurrentChar = "";
// Review substrings starting at each index
for (let i = 0; i < str.length; i++) {
sPossibleLongestNonrepeatingSubstring = "";
// Fixed starting point (i), now keep adding nonrepeating characters
for (let j = i; j < str.length; j++) {
sCurrentChar = str.charAt(j);
// Char is contained -> stop, longest nonrepeating substring from starting point (i) found
if (sPossibleLongestNonrepeatingSubstring.indexOf(sCurrentChar) >= 0) {
break;
}
// Loop not broken because char is not yet contained -> increase result string
sPossibleLongestNonrepeatingSubstring += sCurrentChar;
}
// Retrieved longest nonrepeating substring from starting point (i), compare length and refresh global result if appropriate
if (sPossibleLongestNonrepeatingSubstring.length > sLongestNonrepeatingSubstring.length) {
sLongestNonrepeatingSubstring = sPossibleLongestNonrepeatingSubstring;
}
}
return sLongestNonrepeatingSubstring.length;
}
module.exports = {
longestNonrepeatingSubstring: longestNonrepeatingSubstring
};