Skip to content

Commit f1759aa

Browse files
committed
Add solution #3
1 parent a95eca5 commit f1759aa

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
|:---|:---|:---|
99
1|[Two Sum](./0001-two-sum.js)|Easy|
1010
2|[Add Two Numbers](./0002-add-two-numbers.js)|Medium|
11+
3|[Longest Substring Without Repeating Characters](./0003-longest-substring-without-repeating-characters.js)|Medium|
1112
4|[Median of Two Sorted Arrays](./0004-median-of-two-sorted-arrays.js)|Hard|
1213
7|[Reverse Integer](./0007-reverse-integer.js)|Easy|
1314
27|[Remove Element](./0027-remove-element.js)|Easy|
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 3. Longest Substring Without Repeating Characters
3+
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
4+
* Difficulty: Medium
5+
*
6+
* Given a string `s`, find the length of the longest substring without repeating characters.
7+
*/
8+
9+
/**
10+
* @param {string} s
11+
* @return {number}
12+
*/
13+
var lengthOfLongestSubstring = function(s) {
14+
const map = {};
15+
let offset = 0;
16+
17+
return s.split('').reduce((max, value, i) => {
18+
offset = map[value] >= offset ? map[value] + 1 : offset;
19+
map[value] = i;
20+
return Math.max(max, i - offset + 1);
21+
}, 0);
22+
};

0 commit comments

Comments
 (0)