Skip to content

Commit 8f38541

Browse files
authored
Create 2781-length-of-the-longest-valid-substring.js
1 parent dec9c08 commit 8f38541

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string} word
3+
* @param {string[]} forbidden
4+
* @return {number}
5+
*/
6+
var longestValidSubstring = function(word, forbidden) {
7+
let setF = new Set(forbidden)
8+
let res = 0, left = 0
9+
for(let i = 0; i < word.length; i++) {
10+
for(let j = Math.max(left, i - 10); j < i + 1; j++) {
11+
if(setF.has(word.slice(j, i + 1))) {
12+
left = j + 1
13+
}
14+
}
15+
res = Math.max(res, i - left + 1)
16+
}
17+
return res
18+
};

0 commit comments

Comments
 (0)