Skip to content

Commit

Permalink
Minimum Insertions to form a Palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
detel committed Aug 1, 2015
1 parent 2f54118 commit c4d07d8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Insertions for Palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def insertionsToFormPalin(string):
n = len(string)
dp = [[0]*n for _ in xrange(n)]

for gap in xrange(1,n):
l = 0
for h in xrange(gap,n):
if string[l] == string[h]:
dp[l][h] = dp[l+1][h-1]
else:
dp[l][h] = 1 + min(dp[l+1][h],dp[l][h-1])
l += 1

return dp[0][-1]

print insertionsToFormPalin("deepit")

0 comments on commit c4d07d8

Please sign in to comment.