<p>Given a string s, return <em>the number of different non-empty palindromic subsequences in</em> <code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A subsequence of a string is obtained by deleting zero or more characters from the string.</p>

<p>A sequence is palindromic if it is equal to the sequence reversed.</p>

<p>Two sequences <code>a<sub>1</sub>, a<sub>2</sub>, ...</code> and <code>b<sub>1</sub>, b<sub>2</sub>, ...</code> are different if there is some <code>i</code> for which <code>a<sub>i</sub> != b<sub>i</sub></code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bccb&quot;
<strong>Output:</strong> 6
<strong>Explanation:</strong> The 6 different non-empty palindromic subsequences are &#39;b&#39;, &#39;c&#39;, &#39;bb&#39;, &#39;cc&#39;, &#39;bcb&#39;, &#39;bccb&#39;.
Note that &#39;bcb&#39; is counted only once, even though it occurs twice.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba&quot;
<strong>Output:</strong> 104860361
<strong>Explanation:</strong> There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10<sup>9</sup> + 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s[i]</code> is either <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, <code>&#39;c&#39;</code>, or <code>&#39;d&#39;</code>.</li>
</ul>

class Solution {
public:
    int countPalindromicSubsequences(string S) {
       int i, j, n = S.size();
        vector<vector<int>> dp(n, vector<int>(n,0));
        for(i = 0; i < n; i++){
            dp[i][i] = 1;
            //every single char is palindromic
        }
        for(int k = 1; k < n; k++){
            for(i = 0; i < n - k; i++){
                j = i+k;
                if(S[i] == S[j]){
                    int low = i+1, high = j-1;
                    while(low <= high && S[low] != S[i]) low++;
                    while(low <= high && S[high] != S[j]) high--;
                    if(low > high){
                        //there are all different characters between s[i] and s[j]
                        // without taking s[i] and s[j] there were dp[i+1][j-1] plaindromic subseq no we have all palindromic subseq before + s[i] + all palindromic subseq before + s[j] = dp[i + 1][j - 1] * 2 + 1 for s[i] && 1 for s[i]s[j]
                        
                        dp[i][j] = dp[i + 1][j - 1] * 2 + 2;  
                    }else if(low == high){
                         //there's only one character at centre that is = s[i]
                        //there are all different characters between s[i] and s[j]
                        // without taking s[i] and s[j] there were dp[i+1][j-1] plaindromic subseq no we have all palindromic subseq before + s[i] + all palindromic subseq before + s[j] = dp[i + 1][j - 1] * 2 + 1 for s[i]s[j] as s[i] is already counted before
                         dp[i][j] = dp[i + 1][j - 1] * 2 + 1;
                    }else{
                        //characters are repeating
                        //eg aaabcbaaa ans is ans of aaabcbaaa - ans of aabcbaa
                         dp[i][j] = dp[i + 1][j - 1] * 2 - dp[low + 1][high - 1]; 
                    }
                }else{
                  dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1]; 
                }
                dp[i][j] = dp[i][j] < 0 ? dp[i][j] + 1000000007 : dp[i][j] % 1000000007;
            }
        }
        return dp[0][n-1];
    }
};
28508