Skip to content

Instantly share code, notes, and snippets.

@erikseulean
Created February 13, 2018 16:02
Show Gist options
  • Select an option

  • Save erikseulean/bab62674b35da1263eaac0751ff3956a to your computer and use it in GitHub Desktop.

Select an option

Save erikseulean/bab62674b35da1263eaac0751ff3956a to your computer and use it in GitHub Desktop.
'''
compute the minimum number of removals in order to make the string palindrome
'''
def palindromic_substring(word):
dp = [[-1 for _ in range(len(word) + 1)] for _ in range(len(word)+1)]
def count_nr_chars(word, i, j, count):
if dp[i][j] != -1:
return dp[i][j]
if i >= j:
return count
if word[i] == word[j]:
value = count_nr_chars(word, i + 1, j - 1, count)
else:
value = min(count_nr_chars(word, i + 1, j, count + 1),
count_nr_chars(word, i, j - 1, count + 1),
count_nr_chars(word, i + 1, j - 1, count + 2))
dp[i][j] = value
return dp[i][j]
return count_nr_chars(word, 0, len(word) - 1, 0)
print(palindromic_substring('zmlbc'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment