Created
February 13, 2018 16:02
-
-
Save erikseulean/bab62674b35da1263eaac0751ff3956a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| 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