-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLongestSubstring.js
More file actions
51 lines (28 loc) · 819 Bytes
/
Copy pathLongestSubstring.js
File metadata and controls
51 lines (28 loc) · 819 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
function lengthOflongestSubstring(s){
let set = [];
let maxLength = 0;
for(let right = 0; right < s.length; right++){
if(set.includes(s[right])){
while(set[0] !== s[right]){
set.shift();
}
set.shift();
}
set.push(s[right]);
maxLength = Math.max(maxLength,set.length)
}
return maxLength
}
function version2(str){
let set = new Set();
let left = 0;
for(let right = 0; right < str.length; right++){
while(set.has(str[right])){
set.delete(str[left++])
}
set.add(str[right]);
}
const strLength = [...set].length
return `The substring is ${[...set]} and the string length is ${strLength}`;
}
console.log(version2("ababaacccbddefttttyui"))