311. Count Palindromic Subsequences

You are given a string s consisting of lowercase English letters.

Return the total number of palindromic subsequences present in the string.

A subsequence is a sequence that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

A palindromic subsequence is one that reads the same forward and backward.

Example 1:

Input: s = "abcd"

Output: 4

Explanation: Only the single characters are palindromic: "a", "b", "c", "d"

Example 2:

Input: s = "aab"

Output: 4

Explanation: The palindromic subsequences are: "a", "a", "b", and "aa"

Now Your Turn!

Pick the correct output for the given input

Input: s = "aaa"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= s.length <= 30
  • All characters in s are lowercase English letters.
  • The answer can be large, but you do not need to take modulo.
  • Subsequences need not be distinct — count all valid palindromic subsequences.
0
class Solution {
public:
int countPalSubseq(const string& s) {
// Your code goes here
}
};
 
Test Case

Input:

S