Skip to content

Commit d819cc5

Browse files
committed
2. Longest Substring Without Repeating Characters
1 parent 6dd8755 commit d819cc5

File tree

1 file changed

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

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* hash table + two pointer
5+
*
6+
* n = length of s
7+
* time complexity: O(n)
8+
* space complexity: O(n)
9+
*/
10+
var lengthOfLongestSubstring = function (s) {
11+
const map = new Map();
12+
let answer = 0;
13+
let start = 0;
14+
let end = 0;
15+
for (let i = 0; i < s.length; i++) {
16+
if (map.has(s[i])) start = Math.max(map.get(s[i]) + 1, start);
17+
map.set(s[i], i);
18+
end += 1;
19+
answer = Math.max(answer, end - start);
20+
}
21+
22+
return answer;
23+
};

0 commit comments

Comments
 (0)