diff --git a/Dynamic_Programming/min_insertion_for_palindrome.cpp b/Dynamic_Programming/min_insertion_for_palindrome.cpp new file mode 100644 index 00000000..2c48fa89 --- /dev/null +++ b/Dynamic_Programming/min_insertion_for_palindrome.cpp @@ -0,0 +1,22 @@ +// A Dynamic Programming based program to find +// minimum number insertions needed to make a +// string palindrome +#include +using namespace std; + + +int findMinInsertionsDP(char str[], int n) +{ + int table[n][n], l, h, gap; + + memset(table, 0, sizeof(table)); + + for (gap = 1; gap < n; ++gap) + for (l = 0, h = gap; h < n; ++l, ++h) + table[l][h] = (str[l] == str[h])? + table[l + 1][h - 1] : + (min(table[l][h - 1], + table[l + 1][h]) + 1); + + return table[0][n - 1]; +}