-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit Distance
More file actions
121 lines (96 loc) · 4.19 KB
/
Edit Distance
File metadata and controls
121 lines (96 loc) · 4.19 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Prob: https://leetcode.com/problems/edit-distance/
_______________________________________________________________________________________________________________________________________
Explanation:
1- https://www.youtube.com/watch?v=F469rXsQSP4&list=PLpO3gASfJEIJRnNG4q6QoHAYAATo466a_&index=17&t=0s
2 - https://www.youtube.com/watch?v=OCsF6u-bLBc
3- https://leetcode.com/problems/edit-distance/discuss/159295/Python-solutions-and-intuition
base case: word1 = "" or word2 = "" => return length of other string
recursive case: word1[0] == word2[0] => recurse on word1[1:] and word2[1:]
recursive case: word1[0] != word2[0] => recurse by inserting, deleting, or replacing
________________________________________________________________________________________________________________________________________
Code:
-----
1- Recursive:
----------------
def edit(word1,word2):
if len(word1) == 0:
return len(word2)
if len(word2) == 0:
return len(word1)
if word1[0] == word2[0]:
return edit(word1[1:m],word2[1:n])
else:
insert = edit(word1,word2[1:n])
deletion = edit(word1[1:m],word2)
replace = edit(word1[1:m],word2[1:n])
return 1 + min(insert,deletion,replace)
return edit(word1,word2)
*********************************************************************************************************************************
Copied Solution:
_________________
class Solution:
def minDistance(self, word1, word2):
"""Naive recursive solution"""
if not word1 and not word2:
return 0
if not word1:
return len(word2)
if not word2:
return len(word1)
if word1[0] == word2[0]:
return self.minDistance(word1[1:], word2[1:])
insert = 1 + self.minDistance(word1, word2[1:])
delete = 1 + self.minDistance(word1[1:], word2)
replace = 1 + self.minDistance(word1[1:], word2[1:])
return min(insert, replace, delete)
_________________________________________________________________________________________________________________________________________
2 - DP solution:
------------------
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
dp = []
for _ in range(m+1):
dp.append([0]*(n+1))
for i in range(m+1):
for j in range(n+1):
# Base case is to handle when one of the string will be empty
# make a table and check when one is empty and how many steps
# it will be required to make it equal-> normally del or insert ops
if i == 0:
dp[i][j] = j
if j == 0:
dp[i][j] = i
for i in range(1,m+1):
for j in range(1,n+1):
# if two char are same no operation will be done move ahead
if word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
# else we have 3 choices: insertion,del,replace
# select the one with min number of steps
dp[i][j] = 1 + min(dp[i-1][j-1],dp[i][j-1],dp[i-1][j])
return dp[-1][-1]
################################################################################################################################
MEMO Solution:
---------------
class Solution:
def minDistance(self, word1, word2, i, j, memo):
"""Memoized solution"""
if i == len(word1) and j == len(word2):
return 0
if i == len(word1):
return len(word2) - j
if j == len(word2):
return len(word1) - i
if (i, j) not in memo:
if word1[i] == word2[j]:
ans = self.minDistance2(word1, word2, i + 1, j + 1, memo)
else:
insert = 1 + self.minDistance2(word1, word2, i, j + 1, memo)
delete = 1 + self.minDistance2(word1, word2, i + 1, j, memo)
replace = 1 + self.minDistance2(word1, word2, i + 1, j + 1, memo)
ans = min(insert, delete, replace)
memo[(i, j)] = ans
return memo[(i, j)]