Skip to content

Commit bf225cf

Browse files
Jeehay28Jeehay28
authored andcommitted
Add longest-substring-without-repeating-characters solution in TS
1 parent 1a4b697 commit bf225cf

File tree

1 file changed

+21
-0
lines changed
  • longest-substring-without-repeating-characters

1 file changed

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

0 commit comments

Comments
 (0)