-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs43.js
More file actions
25 lines (25 loc) · 695 Bytes
/
Copy pathjs43.js
File metadata and controls
25 lines (25 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var longestIdealString = function(s, k) {
let n = s.length, longest = 0;
let maxLength = new Array(n).fill(0);
const dfs = (i) => {
if(i === n) return 0;
if(maxLength[i] > 0) return maxLength[i];
let cur = 1, curCode = s.charCodeAt(i);
for(let j = i+1; j < n; j++){
if(Math.abs(s.charCodeAt(j) - curCode) <= k){
cur = Math.max(cur, dfs(j) + 1);
}
if(s[j] === s[i]) break;
}
return maxLength[i] = cur;
}
for(let i = 0; i < n; i++){
longest = Math.max(longest, dfs(i));
}
return longest;
};