title: Count Palindromic Subsequence Given String
signature: fun countPalindromicSubsequenceGivenString(str: String): Int {
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 ];
  }
question_info.test_list: [{"idx": 0, "outputs": 1, "inputs": {"str": "R"}}, {"idx": 1, "outputs": 10, "inputs": {"str": "2956350"}}, {"idx": 2, "outputs": 2798, "inputs": {"str": "11100111110101"}}, {"idx": 3, "outputs": 20, "inputs": {"str": "TZTDLIIfAD"}}, {"idx": 4, "outputs": 2, "inputs": {"str": "98"}}, {"idx": 5, "outputs": 270, "inputs": {"str": "1100100001"}}, {"idx": 6, "outputs": 8, "inputs": {"str": "oKwGeatf"}}, {"idx": 7, "outputs": 2, "inputs": {"str": "19"}}, {"idx": 8, "outputs": 563, "inputs": {"str": "00010110100"}}, {"idx": 9, "outputs": 3, "inputs": {"str": "Cyq"}}]
question_info.extension: kts
question_info.test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(str: String, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(str), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver("R", 1)
      println("TEST-0...${result}")
      
      result = driver("2956350", 10)
      println("TEST-1...${result}")
      
      result = driver("11100111110101", 2798)
      println("TEST-2...${result}")
      
      result = driver("TZTDLIIfAD", 20)
      println("TEST-3...${result}")
      
      result = driver("98", 2)
      println("TEST-4...${result}")
      
      result = driver("1100100001", 270)
      println("TEST-5...${result}")
      
      result = driver("oKwGeatf", 8)
      println("TEST-6...${result}")
      
      result = driver("19", 2)
      println("TEST-7...${result}")
      
      result = driver("00010110100", 563)
      println("TEST-8...${result}")
      
      result = driver("Cyq", 3)
      println("TEST-9...${result}")
      
          
  }
  
  main()
question_info.entry_fn_name: countPalindromicSubsequenceGivenString
question_info.entry_cls_name: Solution
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: kotlinc -script __FILENAME__ -no-reflect -nowarn