title: Count Palindromic Subsequence Given String
signature: func countPalindromicSubsequenceGivenString(str string) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(str string, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(str),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver("R", 1)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver("2956350", 10)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver("11100111110101", 2798)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver("TZTDLIIfAD", 20)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver("98", 2)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver("1100100001", 270)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver("oKwGeatf", 8)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver("19", 2)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver("00010110100", 563)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver("Cyq", 3)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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 ];
  }