Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Dynamic_Programming/min_insertion_for_palindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// A Dynamic Programming based program to find
// minimum number insertions needed to make a
// string palindrome
#include <bits/stdc++.h>
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];
}