Skip to content

Commit 94a7a97

Browse files
authored
Create 2484-count-palindromic-subsequences.js
1 parent 24d2e12 commit 94a7a97

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const initialize2DArray = (n, m) => {
2+
let d = []
3+
for (let i = 0; i < n; i++) {
4+
let t = Array(m).fill(0)
5+
d.push(t)
6+
}
7+
return d
8+
}
9+
10+
const mod = 1e9 + 7
11+
/**
12+
* @param {string} s
13+
* @return {number}
14+
*/
15+
const countPalindromes = (s) => {
16+
let res = 0,
17+
n = s.length,
18+
cnt = Array(10).fill(0)
19+
for (let i = 0; i < n; i++) {
20+
let tot = 0
21+
for (let j = n - 1; j > i; j--) {
22+
if (s[i] == s[j]) {
23+
res += tot * (j - i - 1)
24+
res %= mod
25+
}
26+
tot += cnt[s[j] - "0"]
27+
}
28+
cnt[s[i] - "0"]++
29+
}
30+
return res
31+
}

0 commit comments

Comments
 (0)