Skip to content

Commit c87dea2

Browse files
authored
Update 3-Longest-Substring-Without-Repeating-Characters.js
Refactor 3 Longest Substring Without Repeating Characters
1 parent e7722e5 commit c87dea2

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

javascript/3-Longest-Substring-Without-Repeating-Characters.js

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
* @param {string} s
55
* @return {number}
66
*/
7-
var lengthOfLongestSubstring = function(s, map = new Map()) {
8-
let [ left, right, max ] = [ 0, 0, 0];
97

10-
while (right < s.length) {
11-
const rightChar = s[right];
12-
13-
const canSlide = map.has(rightChar);
14-
if (canSlide) {
15-
const rightIndex = map.get(rightChar) + 1;
16-
17-
left = Math.max(left, rightIndex);
8+
var lengthOfLongestSubstring = function(s) {
9+
map = new Map()
10+
l = 0
11+
res = 0
12+
13+
for (let r=0; r < s.length; r++) {
14+
15+
while (map.has(s[r])) {
16+
map.delete(s[l])
17+
l++
1818
}
19-
20-
const window = (right - left) + 1;
21-
22-
max = Math.max(max, window);
23-
map.set(rightChar, right);
24-
right++;
19+
20+
map.set(s[r], s[r])
21+
res = Math.max(res, r-l + 1)
22+
2523
}
26-
27-
return max;
24+
return res
25+
2826
};

0 commit comments

Comments
 (0)