forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_of_deletions_to_make_pallindrome.py
62 lines (50 loc) · 1.39 KB
/
Number_of_deletions_to_make_pallindrome.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Python3 implementation to find
# minimum number of deletions
# to make a string palindromic
INT_MAX = 99999999999
def noofDeletions(s):
# reverse of input string
revInput = s[::-1]
# DP table for storing
# edit distance of string and reverse.
n = len(s)
dp = [[-1 for _ in range(n + 1)]
for __ in range(n + 1)]
for i in range(n + 1):
dp[0][i] = i
dp[i][0] = i
# Find edit distance between
# input and revInput considering
# only delete operation.
for i in range(1, n + 1):
for j in range(1, n + 1):
if s[i - 1] == revInput[j - 1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j],
dp[i][j - 1])
# Go from bottom left to top right
# and find the minimum
res = INT_MAX
i, j = n, 0
while i >= 0:
res = min(res, dp[i][j])
if i < n:
res = min(res, dp[i + 1][j])
if i > 0:
res = min(res, dp[i - 1][j])
i -= 1
j += 1
return res
# Driver Code
if __name__ == "__main__":
string = input('Enter string: ')
print(noofDeletions(string))
# Time Complexity: O(n*n)
# Space complexity: O(n*n)
# Example 1:
# input : malasiyala
# output: 3
# Example 2:
# input : acdbdba
# output: 1