Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Files
Expand file tree
master
Breadcrumbs
algorithm
/
javascript
/
code
/
string
/
longestSubstring.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
26 lines (24 loc) · 710 Bytes
master
Breadcrumbs
algorithm
/
javascript
/
code
/
string
/
longestSubstring.js
Copy path
Top
File metadata and controls
Code
Blame
26 lines (24 loc) · 710 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Given a string, find the length of the longest substring without repeating characters.
* @param {string} s
* @returns {number} The length of the longest substring without repeating characters
*/
function longestSubstring(s) {
const len = s.length;
let start = 0;
let end = 1;
let maxLength = 0;
while (end <= len) {
let currentSubstring = s.substring(start, end);
maxLength = Math.max(maxLength, currentSubstring.length);
if (currentSubstring.includes(s[end])) {
do {
start++;
currentSubstring = s.substring(start, end);
} while (currentSubstring.includes(s[end]));
}
end++;
}
return maxLength;
}
module.exports = { longestSubstring };
You can’t perform that action at this time.