title: Minimum Steps To Delete A String After Repeated Deletion Of Palindrome Substrings
signature: class Solution {
  
      public int MinimumStepsToDeleteAStringAfterRepeatedDeletionOfPalindromeSubstrings(string str) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(string str, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(str); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  "YCtLQtHLwr", 
                  8
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  "47713514383248", 
                  7
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  "0100101001111", 
                  3
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  "XfdIYVn", 
                  7
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  "45499225407", 
                  5
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  "000100111001", 
                  2
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  "ZoUQhQwoap", 
                  6
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  "18579027952", 
                  7
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  "00000001111", 
                  2
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  "JD", 
                  2
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
source_py: def minimum_steps_to_delete_a_string_after_repeated_deletion_of_palindrome_substrings(str):
      N = len(str)
      dp = [[0 for x in range((N + 1))] for y in range((N + 1))]
      for l in range(1, (N + 1)):
          i = 0
          j = (l - 1)
          while (j < N):
              if (l == 1):
                  dp[i][j] = 1
              else:
                  dp[i][j] = (1 + dp[(i + 1)][j])
                  if (str[i] == str[(i + 1)]):
                      dp[i][j] = min((1 + dp[(i + 2)][j]), dp[i][j])
                  for K in range((i + 2), (j + 1)):
                      if (str[i] == str[K]):
                          dp[i][j] = min((dp[(i + 1)][(K - 1)] + dp[(K + 1)][j]), dp[i][j])
              i += 1
              j += 1
      return dp[0][(N - 1)]
source_cpp: int minimum_steps_to_delete_a_string_after_repeated_deletion_of_palindrome_substrings(string str) {
    int N = str . length ( );
    int dp [ N + 1 ] [ N + 1 ];
    for ( int i = 0;
    i <= N;
    i ++ ) for ( int j = 0;
    j <= N;
    j ++ ) dp [ i ] [ j ] = 0;
    for ( int len = 1;
    len <= N;
    len ++ ) {
      for ( int i = 0, j = len - 1;
      j < N;
      i ++, j ++ ) {
        if ( len == 1 ) dp [ i ] [ j ] = 1;
        else {
          dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ];
          if ( str [ i ] == str [ i + 1 ] ) dp [ i ] [ j ] = min ( 1 + dp [ i + 2 ] [ j ], dp [ i ] [ j ] );
          for ( int K = i + 2;
          K <= j;
          K ++ ) if ( str [ i ] == str [ K ] ) dp [ i ] [ j ] = min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ], dp [ i ] [ j ] );
        }
      }
    }
    return dp [ 0 ] [ N - 1 ];
  }