Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e6f4c1d

Browse files
committedJan 24, 2025·
longest-substring-without-repeating-characters
1 parent 2ab0ddf commit e6f4c1d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 3. Longest Substring Without Repeating Characters
3+
* Given a string s, find the length of the longest substring without repeating characters.
4+
*
5+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
6+
*
7+
*/
8+
9+
// O(n^2) time
10+
// O(n) space
11+
function lengthOfLongestSubstring(s: string): number {
12+
let result = 0;
13+
14+
for (let i = 0; i < s.length; i++) {
15+
let set = new Set();
16+
let substring = 0;
17+
18+
for (let j = i; j < s.length; j++) {
19+
if (set.has(s[j])) {
20+
break;
21+
}
22+
23+
set.add(s[j]);
24+
substring++;
25+
}
26+
27+
result = Math.max(result, substring);
28+
}
29+
30+
return result;
31+
}

0 commit comments

Comments
 (0)
Please sign in to comment.