dp
of size n x n
to zero, where n
is the length of the string s
.next
and prev
arrays to find the next and previous occurrences of each character.n
.i
.i
, calculate the ending index j
.s[i]
equals s[j]
, find the range within i
and j
where the character does not change.dp[i][j]
based on the number of palindromic subsequences within the inner substring and the occurrences of the current character.10^9 + 7
.