Skip to content

Commit 03f758e

Browse files
committed
https://leetcode.cn/problems/maximum-number-of-non-overlapping-palindrome-substrings
1 parent cfe3861 commit 03f758e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ https://leetcode.cn/problems/magical-string/
131131

132132
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/
133133

134+
https://leetcode.cn/problems/maximum-number-of-non-overlapping-palindrome-substrings
135+
134136
https://leetcode.cn/problems/nge-tou-zi-de-dian-shu-lcof
135137

136138
https://leetcode.cn/problems/count-of-matches-in-tournament/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default function maxPalindromes(s: string, k: number): number {
2+
const n = s.length;
3+
const f: number[] = Array(n + 1).fill(0);
4+
for (let i = 0; i < 2 * n - 1; ++i) {
5+
let l = Math.floor(i / 2),
6+
r = l + (i % 2); // 中心扩展法
7+
f[l + 1] = Math.max(f[l + 1], f[l]);
8+
for (; l >= 0 && r < n && s[l] == s[r]; --l, ++r) {
9+
if (r - l + 1 >= k) f[r + 1] = Math.max(f[r + 1], f[l] + 1);
10+
}
11+
}
12+
return f[n];
13+
}

0 commit comments

Comments
 (0)