Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 713b1b6

Browse files
Longest Substring Without Repeating Characters
1 parent 49faa0b commit 713b1b6

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1+
/**
2+
* @param {string} s Input string.
3+
* @return {number} Length of longest substring without repeating characters.
4+
* @summary Longest Substring Without Repeating Characters {@link https://leetcode.com/problems/longest-substring-without-repeating-characters/}
5+
* @description Given a string, find length of its longest substring without repeating characters.
6+
* Space O(n) - hash object storing data will have no more than n elements.
7+
* Time O(n) - one iteration of n elements.
8+
*/
19
const lengthOfLongestSubstring = s => {
2-
letlongest = 0,
3-
current= 0,
4-
hashMap={};
10+
constcharMap = {};
11+
letmaxLength= 0;
12+
letcurrentStart=0;
513

614
for (let index = 0; index < s.length; index++) {
7-
if (!hashMap[s[index]]) {
8-
hashMap[s[index]] = true;
9-
current++;
10-
} else {
11-
if (current > longest) {
12-
longest = current;
13-
}
15+
const c = s[index];
1416

15-
current=0;
16-
index--;
17-
hashMap = {};
17+
if(charMap[c]!==undefined&&charMap[c]>=currentStart){
18+
maxLength=Math.max(maxLength,index-currentStart);
19+
currentStart = charMap[c]+1;
1820
}
1921

20-
if (current > longest) {
21-
longest = current;
22-
}
22+
charMap[c] = index;
2323
}
2424

25-
return longest;
25+
return Math.max(maxLength,s.length-currentStart);
2626
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /