title: Dynamic Programming Set 28 Minimum Insertions To Form A Palindrome
signature: function dynamicProgrammingSet28MinimumInsertionsToFormAPalindrome(str: Array<string>, l: number, h: number): number {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value: any): string{
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual: number, expected: number): boolean {
      return convertToString(actual) === convertToString(expected);
  }
  
  function driver(str: Array<string>, l: number, h: number, expected:number): string {
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(str, l, h),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver(['F', 'F', 'J', 'K', 'K', 'L', 'P', 'S', 'T', 'V', 'W', 'Y', 'b', 'd', 'j', 'l', 't', 'u', 'x', 'y'], 11, 11, 0);
    console.log(`TEST-0...${result}`);
  
    result = driver(['0', '1', '8', '8', '8', '4', '4', '3', '9', '6', '5', '2', '8', '2', '0', '2', '6', '0', '7', '7', '3', '2', '4', '5', '9', '7', '2', '4', '1', '8', '7', '9', '8', '0', '8', '5', '4', '2', '3'], 19, 22, 3);
    console.log(`TEST-1...${result}`);
  
    result = driver(['1'], 0, 0, 0);
    console.log(`TEST-2...${result}`);
  
    result = driver(['g', 'y', 'r', 'M', 'v', 'z', ' ', 'k', 'S', 'P', 'x', 'p', 'E', 'z', 'T', 'K', 'k', 'B', 's', 'P', 'p', 'e', 'G', 't', 'r', 'M', 'p', ' ', 'H', 'a'], 24, 27, 3);
    console.log(`TEST-3...${result}`);
  
    result = driver(['0', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '3', '3', '3', '3', '3', '4', '4', '4', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '6', '6', '7', '8', '8', '8', '9', '9'], 33, 34, 0);
    console.log(`TEST-4...${result}`);
  
    result = driver(['X', 'B', 'E', 'v', 'K', 't', 'k', 'K', 'Q', 's', 'V', 'N', 'l', 'k', 'T', 'N', 'J', 'z', 'f', 'p', 'J', 'g', 'S', 'P', 'M', 'b', 'H', 'L', 'v', 'E', 'A', 'n', 'D', 'U', 'c', 's', 'M', 'Q', 'P', 'g', 'g', ' '], 26, 27, 1);
    console.log(`TEST-5...${result}`);
  
  };
  
  main();
source_py: def dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome(str, l, h):
      if (l > h):
          return sys.maxsize
      if (l == h):
          return 0
      if (l == (h - 1)):
          return (0 if (str[l] == str[h]) else 1)
      if (str[l] == str[h]):
          return dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome(str, (l + 1), (h - 1))
      else:
          return (min(dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome(str, l, (h - 1)), dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome(str, (l + 1), h)) + 1)
source_cpp: int dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome(vector<char> str, int l, int h) {
    if ( l > h ) return INT_MAX;
    if ( l == h ) return 0;
    if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1;
    return ( str [ l ] == str [ h ] ) ? dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome ( str, l + 1, h - 1 ) : ( min ( dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome ( str, l, h - 1 ), dynamic_programming_set_28_minimum_insertions_to_form_a_palindrome ( str, l + 1, h ) ) + 1 );
  }