title: Dynamic Programming Set 28 Minimum Insertions To Form A Palindrome
signature: int dynamicProgrammingSet28MinimumInsertionsToFormAPalindrome(vector<char> str, int l, int h) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(vector<char> str, int l, int h, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(str, l, h), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string 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); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({'1'}, 0, 0, 0); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      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); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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 );
  }