-
Notifications
You must be signed in to change notification settings - Fork 0
/
countSubstrings1.ts
45 lines (37 loc) · 1001 Bytes
/
countSubstrings1.ts
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//动态规划
function countSubstrings(s: string): number {
const dp = Array(s.length);
let count = 0;
for (let i = s.length - 1; i >= 0; i--) {
dp[i] = [];
for (let j = i; j < s.length; j++) {
if (i === j) {
dp[i][j] = true;
} else if (j - i === 1) {
dp[i][j] = s[i] === s[j];
} else {
dp[i][j] = dp[i+1][j-1] && s[i] === s[j];
}
if (dp[i][j]) {
count++;
}
}
}
return count;
};
//中心扩展法 妙啊
function countSubstrings(s: string): number {
let count = 0;
const counter = (s: string, start: number, end: number): void => {
while (start >= 0 && end < s.length && s[start] === s[end]) {
count++;
start--;
end++;
}
}
for (let i = 0; i < s.length; i++) {
counter(s, i, i);
counter(s, i, i+1);
}
return count;
}