Skip to content

Commit 886e500

Browse files
committed
03-Longest-Substring-Without-Repeating-Characters initial
1 parent 1dfaacd commit 886e500

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function (s) {
6+
const lastIndex = new Map();
7+
let maxLen = 0;
8+
let i = 0;
9+
10+
for (let j = 0; j < s.length; j++) {
11+
const ch = s[j];
12+
if (lastIndex.has(ch) && lastIndex.get(ch) >= i) {
13+
i = lastIndex.get(ch) + 1;
14+
}
15+
lastIndex.set(ch, j);
16+
maxLen = Math.max(maxLen, j - i + 1);
17+
}
18+
19+
return maxLen;
20+
};

0 commit comments

Comments
 (0)