Skip to content

Commit 4af617b

Browse files
committed
solved: 3
1 parent 8c68735 commit 4af617b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function lengthOfLongestSubstring(str: string): number {
2+
let ans = 0;
3+
4+
let pos = -1;
5+
6+
const map = new Map<string, number>();
7+
8+
Array.from(str).forEach((char, index) => {
9+
const d = map.get(char);
10+
11+
/** if map has char previous index and the candidate left position is less than the previous index, update the left position */
12+
if (d != null && d > pos) {
13+
pos = d;
14+
}
15+
16+
map.set(char, index);
17+
18+
ans = Math.max(ans, index - pos);
19+
});
20+
return ans;
21+
}

note/3.PNG

125 KB
Loading

0 commit comments

Comments
 (0)