Skip to content

Commit 19460fd

Browse files
authored
Create longest-substring-without-repeating-characters.ts
1 parent 3559d84 commit 19460fd

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
if (s.length === 0) {
3+
return 0;
4+
}
5+
6+
let maxLength = 0;
7+
let left = 0;
8+
const uniqueChars = new Set<string>();
9+
10+
for (let right = 0; right < s.length; right++) {
11+
while (uniqueChars.has(s[right])) {
12+
uniqueChars.delete(s[left]);
13+
left++;
14+
}
15+
uniqueChars.add(s[right]);
16+
maxLength = Math.max(maxLength, right - left + 1);
17+
}
18+
19+
return maxLength;
20+
}
21+

0 commit comments

Comments
 (0)