title: Count Palindromic Subsequence Given String
signature: count_palindromic_subsequence_given_string <- function(str) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(str, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(str), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          "R", 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          "2956350", 
          10L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          "11100111110101", 
          2798L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          "TZTDLIIfAD", 
          20L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          "98", 
          2L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          "1100100001", 
          270L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          "oKwGeatf", 
          8L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          "19", 
          2L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          "00010110100", 
          563L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          "Cyq", 
          3L
      )))
  
  }
  
  main()
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 ];
  }