# Count Different Palindromic Subsequences

Given a string S, find the number of different non-empty palindromic subsequences in S, and **return that number modulo `10^9 + 7`.**

A subsequence of a string S is obtained by deleting 0 or more characters from S.

A sequence is palindromic if it is equal to the sequence reversed.

Two sequences `A_1, A_2, ...` and `B_1, B_2, ...` are different if there is some `i` for which `A_i != B_i`.

**Example 1:**<br>

```
Input: 
S = 'bccb'
Output: 6
Explanation: 
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.
```

**Example 2:**<br>

```
Input: 
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation: 
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
```

**Note:**

The length of `S` will be in the range `[1, 1000]`.

Each character `S[i]` will be in the set `{'a', 'b', 'c', 'd'}`.

很有难度的一道题，

{% embed url="<http://www.cnblogs.com/grandyang/p/7942040.html>" %}

{% embed url="<https://www.youtube.com/watch?v=UjiFFYU3EKM&t=1087s>" %}

时间和空间复杂度 o(n^2)&#x20;

```java
class Solution {
   

    public int countPalindromicSubsequences(String S) {
       int n = S.length();
        
        int[][] dp = new int[n][n];
        int M = 1000000007;
        //长读为1的字符串 是一个回文
        for(int i = 0; i < n; i++){
            dp[i][i] = 1;
        }
        
        for(int len = 1; len < n;len++){
            for(int i = 0 ; i < n - len; i++){
                int j = i + len;
                
                if(S.charAt(j) == S.charAt(i)){
                    int left = i + 1;
                    int right = j -1;
                    
                    //从s[i] 的下一个，到s[j]的前一个开始找，遇到和s[i]相同的，就停止，停止时，如果left 小于right，说明遇到了和s[i]一样的字符，
                    //如果left 大于 right，说明没有遇到相同的字符，while循环是根据left 《= right 终止的，如果left == right，说明只有一个和s[i]相同的字符
                    while(left<= right && S.charAt(left) != S.charAt(j)) left++;
                    while(left<= right && S.charAt(right) != S.charAt(j)) right--;
                    
                    //中间没有和收尾相同的字符
                    if(left > right){
                        dp[i][j] = dp[i+1][j-1]*2 + 2;
                    }
                    else if(left == right){
                        dp[i][j] = dp[i+1][j-1]*2 + 1;
                    }
                    else{
                        
                        //重复计算的个数，就是两个重复相同字符之间所有字符的回文个数
                        dp[i][j] = dp[i+1][j-1]*2 - dp[left+1][right-1];
                    }
                }else{
                    dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
                }
                //or
                dp[i][j] = (dp[i][j] + M)%M
                dp[i][j] = (dp[i][j] < 0)? dp[i][j] + M : dp[i][j] % M;
            }
        }
        
        return dp[0][n-1];
    }

    
       
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shuati.gitbook.io/crack-lintcode/linkedin/count-different-palindromic-subsequences.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
