Minimum insertions to make palindrome Today you will be doing another palindrome challenge!

So, your task today is to take a string, and determine the minimum amount of letters required to insert to turn it into a palindrome.

For example, let's take the string fishes.

In this, case the best way would be to add h if, so the result would be 3.

fishe s
     h if
---------
fishehsif


Now let's try with codegolf. Since there is a repeated o, we can just do:

  codeg  o lf
fl     ed c
-------------
flcodegedoclf


to get a result of 5.

Test cases

ppcg -&gt; 2
codegolf -&gt; 5
palindrome -&gt; 9
stackexchange -&gt; 8
programmingpuzzlesandcodegolf -&gt; 20
Python, 112 bytes

d=lambda x,l,h:0if h&lt;l else d(x,l+1,h-1)if x[l]==x[h]else-~min(d(x,l+1,h),d(x,l,h-1));e=lambda x:d(x,0,len(x)-1)


Very inefficient.

Try it online! You have to wait a minute for the last case to finish.

Call with e(&lt;string&gt;, 0, &lt;length of string - 1&gt;), like e("fishes", 0, 5)`.

Ungolfed (sort of) with explanation:

def minInsert(x, l, h):                # Declare func, arugments for x, l, h       # d=lambda x,l,h:
  if l &gt;= h:                           # If l is the same or past h                #                 if h&lt;l
    return 0                           #     then return 0                         #                0
  elif x[l] == x[h]:                   # If first and last character are the same  #                        else             if x[l]==x[h]
    return minInsert(x, l + 1, h - 1)  #     then return the min w/o first &amp; last  #                             d(x,l+1,h-1)
  else:                                # If not, we shave off a character          #                                                      else
    a = minInsert(x, l, h - 1)         #     (last one)                            #                                                                d(x,l+1,h)
    b = minInsert(x, l + 1, h)         #     (first one)                           #                                                                           d(x,l,h-1)
    return min(a, b) + 1               #     and add one for the char we took off  #                                                          -~min(          ,          )