Skip to content

Commit 632a166

Browse files
author
tanjiasheng
committed
Longest Substring Without Repeating Characters
1 parent 7146845 commit 632a166

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function (s) {
6+
const cleanStr = [...new Set(s)].join('');
7+
let cleanStrLen = cleanStr.length;
8+
9+
while (cleanStrLen) {
10+
for (let i = 0; i <= s.length - cleanStrLen; i++) {
11+
const subStr = s.substr(i, cleanStrLen);
12+
13+
if (!subStr.split('').some((char, i) => subStr.indexOf(char) !== i)) {
14+
return cleanStrLen;
15+
}
16+
}
17+
18+
cleanStrLen--;
19+
}
20+
21+
return 0;
22+
};
23+
24+
25+
console.log(lengthOfLongestSubstring('pwwkew'))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
先去重,然后最长的情况下不重复的子串就是去重后字符串的长度N,如果找不到的话依次递减(N--),找到输入字符串的子串中有没有存在长度N的不重复字符串

0 commit comments

Comments
 (0)