-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlengthOfLongestSubstr.js
49 lines (46 loc) · 1.22 KB
/
lengthOfLongestSubstr.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
39
40
41
42
43
44
45
46
47
48
49
/**
* @param {string} s
* @return {number}
*/
// var lengthOfLongestSubstring = function(s) {
// if(!s.length) return 0
// const globalHash = [];
// let hash = {};
// for(let i=0; i<s.length; i++) {
// if(hash[s[i]]) {
// globalHash.push(hash);
// hash = {};
// hash[s[i]] = 1;
// } else {
// hash[s[i]] = 1;
// }
// if(i === s.length-1) {
// globalHash.push(hash)
// }
// }
// console.log(globalHash)
// return Math.max(...globalHash.map(e => Object.keys(e).length))
// };
var lengthOfLongestSubstring = function(s) {
if(!s.length) return 0
let hash = {};
let start = 0;
let max = 0;
for(let i = 0; i<s.length; i++) {
const char = s[i];
hash[char] = hash[char] +1 || 1;
while(hash[char] > 1) {
let leftChar = s[start];
if(hash[leftChar] > 1) {
hash[leftChar]--;
} else {
delete hash[leftChar]
}
start++;
}
max = Math.max(max, i-start+1);
}
return max;
};
console.log(lengthOfLongestSubstring('abcabcbb'))
// console.log(lengthOfLongestSubstring('dvdf'))