title: Count Palindromic Subsequence Given String
signature: int countPalindromicSubsequenceGivenString(string str) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(string str, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(str), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver("R", 1); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver("2956350", 10); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver("11100111110101", 2798); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver("TZTDLIIfAD", 20); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver("98", 2); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver("1100100001", 270); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver("oKwGeatf", 8); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver("19", 2); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver("00010110100", 563); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver("Cyq", 3); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def count_palindromic_subsequence_given_string(str):
      N = len(str)
      cps = [[0 for i in range((N + 2))] for j in range((N + 2))]
      for i in range(N):
          cps[i][i] = 1
      for L in range(2, (N + 1)):
          for i in range(N):
              k = ((L + i) - 1)
              if (k < N):
                  if (str[i] == str[k]):
                      cps[i][k] = ((cps[i][(k - 1)] + cps[(i + 1)][k]) + 1)
                  else:
                      cps[i][k] = ((cps[i][(k - 1)] + cps[(i + 1)][k]) - cps[(i + 1)][(k - 1)])
      return cps[0][(N - 1)]
source_cpp: int count_palindromic_subsequence_given_string(string str) {
    int N = str . length ( );
    int cps [ N + 1 ] [ N + 1 ];
    memset ( cps, 0, sizeof ( cps ) );
    for ( int i = 0;
    i < N;
    i ++ ) cps [ i ] [ i ] = 1;
    for ( int L = 2;
    L <= N;
    L ++ ) {
      for ( int i = 0;
      i < N;
      i ++ ) {
        int k = L + i - 1;
        if ( str [ i ] == str [ k ] ) cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] + 1;
        else cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] - cps [ i + 1 ] [ k - 1 ];
      }
    }
    return cps [ 0 ] [ N - 1 ];
  }