Minimum insertions to form a palindrome | DP | A Naive recursive C # program to find minimum number insertions needed to make a string palindrom ; Recursive function to find minimum number of insertions ; Base Cases ; Check if the first and last characters are same . On the basis of the comparison result , decide which subrpoblem ( s ) to call ; Driver Code
using System ; class GFG { static int findMinInsertions ( char [ ] str , int l , int h ) { if ( l > h ) return int . MaxValue ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( Math . Min ( findMinInsertions ( str , l , h - 1 ) , findMinInsertions ( str , l + 1 , h ) ) + 1 ) ; } public static void Main ( ) { string str = " geeks " ; Console . WriteLine ( findMinInsertions ( str . ToCharArray ( ) , 0 , str . Length - 1 ) ) ; } }