Skip to content

Commit 46ac718

Browse files
committed
longest substring without repeating characters solution
1 parent eb2596c commit 46ac718

File tree

1 file changed

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

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function lengthOfLongestSubstring(s: string): number {
2+
let maxLen = 0;
3+
4+
const used = new Set<string>();
5+
let windowStart = 0;
6+
7+
for (let windowEnd = 0; windowEnd < s.length; windowEnd++) {
8+
const currentCh = s[windowEnd];
9+
while (used.has(currentCh)) {
10+
used.delete(s[windowStart]);
11+
windowStart++;
12+
}
13+
14+
used.add(currentCh);
15+
maxLen = Math.max(maxLen, windowEnd - windowStart + 1);
16+
}
17+
18+
return maxLen;
19+
}

0 commit comments

Comments
 (0)