Given a string of digits s, return the number ofpalindromic subsequences ofshaving length5. Since the answer may be very large, return it modulo10^9 + 7.
Note:
A string is palindromic if it reads the same forward and backward.
A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Input: s ="103301"Output: 2Explanation:
There are 6 possible subsequences of length 5:"10330","10331","10301","10301","13301","03301".Two of them(both equal to "10301") are palindromic.
A palindromic subsequence of length 5 must have the form “abcba”. We can fix the middle character and count the number of ways to choose the first two and last two characters such that they are the same in reverse order. We use prefix and suffix counts for all pairs of digits.
classSolution {
public:int countPalindromes(string s) {
int n = s.size(), mod =1e9+7, ans =0;
vector<vector<int>> pre(10, vector<int>(10)), suf(10, vector<int>(10));
vector<int> cnt1(10), cnt2(10);
for (int i =0; i < n; ++i) {
int d = s[i] -'0';
for (int x =0; x <10; ++x) pre[x][d] += cnt1[x];
cnt1[d]++;
}
for (int i = n-1; i >=0; --i) {
int d = s[i] -'0';
for (int x =0; x <10; ++x) suf[d][x] += cnt2[x];
cnt2[d]++;
}
for (int i =2; i < n-2; ++i) {
for (int a =0; a <10; ++a) {
for (int b =0; b <10; ++b) {
ans = (ans + pre[a][b] * suf[b][a]) % mod;
}
}
}
return ans;
}
};
classSolution {
publicintcountPalindromes(String s) {
int n = s.length(), mod = 1000000007, ans = 0;
int[][] pre =newint[10][10], suf =newint[10][10];
int[] cnt1 =newint[10], cnt2 =newint[10];
for (int i = 0; i < n; i++) {
int d = s.charAt(i) -'0';
for (int x = 0; x < 10; x++) pre[x][d]+= cnt1[x];
cnt1[d]++;
}
for (int i = n-1; i >= 0; i--) {
int d = s.charAt(i) -'0';
for (int x = 0; x < 10; x++) suf[d][x]+= cnt2[x];
cnt2[d]++;
}
for (int i = 2; i < n-2; i++) {
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
ans = (int)((ans + 1L * pre[a][b]* suf[b][a]) % mod);
}
}
}
return ans;
}
}
classSolution {
funcountPalindromes(s: String): Int {
val n = s.length; val mod = 1_000_000_007; var ans = 0val pre = Array(10) { IntArray(10) }; val suf = Array(10) { IntArray(10) }
val cnt1 = IntArray(10); val cnt2 = IntArray(10)
for (i in0 until n) {
val d = s[i] - '0'for (x in0..9) pre[x][d] += cnt1[x]
cnt1[d]++ }
for (i in n-1 downTo 0) {
val d = s[i] - '0'for (x in0..9) suf[d][x] += cnt2[x]
cnt2[d]++ }
for (i in2 until n-2) {
for (a in0..9) for (b in0..9) ans = ((ans + 1L * pre[a][b] * suf[b][a]) % mod).toInt()
}
return ans
}
}
classSolution:
defcountPalindromes(self, s: str) -> int:
n, mod, ans = len(s), 10**9+7, 0 pre = [[0]*10for _ in range(10)]
suf = [[0]*10for _ in range(10)]
cnt1 = [0]*10 cnt2 = [0]*10for d in map(int, s):
for x in range(10):
pre[x][d] += cnt1[x]
cnt1[d] +=1for d in map(int, reversed(s)):
for x in range(10):
suf[d][x] += cnt2[x]
cnt2[d] +=1for a in range(10):
for b in range(10):
for i in range(2, n-2):
ans = (ans + pre[a][b] * suf[b][a]) % mod
return ans
impl Solution {
pubfncount_palindromes(s: String) -> i32 {
let n = s.len();
let modn =1_000_000_007;
let s: Vec<u8>= s.bytes().collect();
letmut pre =vec![vec![0; 10]; 10];
letmut suf =vec![vec![0; 10]; 10];
letmut cnt1 =vec![0; 10];
letmut cnt2 =vec![0; 10];
for&b in&s {
let d = (b -b'0') asusize;
for x in0..10 { pre[x][d] += cnt1[x]; }
cnt1[d] +=1;
}
for&b in s.iter().rev() {
let d = (b -b'0') asusize;
for x in0..10 { suf[d][x] += cnt2[x]; }
cnt2[d] +=1;
}
letmut ans =0i64;
for i in2..n-2 {
for a in0..10 {
for b in0..10 {
ans = (ans + pre[a][b] asi64* suf[b][a] asi64) % modn;
}
}
}
ans asi32 }
}