Guest User

Untitled

a guest
Feb 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. '''
  2. compute the minimum number of removals in order to make the string palindrome
  3. '''
  4.  
  5. def palindromic_substring(word):
  6.  
  7. dp = [[-1 for _ in range(len(word) + 1)] for _ in range(len(word)+1)]
  8.  
  9. def count_nr_chars(word, i, j, count):
  10. if dp[i][j] != -1:
  11. return dp[i][j]
  12.  
  13. if i >= j:
  14. return count
  15.  
  16. if word[i] == word[j]:
  17. value = count_nr_chars(word, i + 1, j - 1, count)
  18. else:
  19. value = min(count_nr_chars(word, i + 1, j, count + 1),
  20. count_nr_chars(word, i, j - 1, count + 1),
  21. count_nr_chars(word, i + 1, j - 1, count + 2))
  22.  
  23. dp[i][j] = value
  24. return dp[i][j]
  25.  
  26. return count_nr_chars(word, 0, len(word) - 1, 0)
  27.  
  28. print(palindromic_substring('zmlbc'))
Add Comment
Please, Sign In to add comment