Skip to content

Commit 8ff2c80

Browse files
author
baochau.dinh
committed
js-concepts: leetcode prob.3 - longest substring without repeated character
1 parent 9483ba2 commit 8ff2c80

File tree

1 file changed

+20
-0
lines changed
  • LeetCode/3. Longest Substring Without Repeating Characters

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var lengthOfLongestSubstring = function(s) {
2+
if (s.length <= 1) return s.length;
3+
let maxCount = 0;
4+
let i = 0, j = 0;
5+
let uniqueSet = new Set();
6+
while (i < s.length && j < s.length) {
7+
if (!uniqueSet.has(s[j])) {
8+
uniqueSet.add(s[j]);
9+
maxCount = Math.max(maxCount, j - i + 1);
10+
j++;
11+
} else {
12+
uniqueSet.delete(s[i]);
13+
i++;
14+
}
15+
}
16+
17+
return maxCount;
18+
};
19+
20+
console.log(lengthOfLongestSubstring('abcabcbb'));

0 commit comments

Comments
 (0)