forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdit_Distance.py
49 lines (43 loc) · 1.93 KB
/
Edit_Distance.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
"""
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
You have the following 3 operations permitted on a word:
a) Insert a character
b) Delete a character
c) Replace a character
"""
class Solution:
# @return an integer
def minDistance(self, word1, word2):
M = len(word1)
N = len(word2)
dp = [ [ 0 for j in range(N+1)] for i in range(M+1)]
for i in range(M+1):
for j in range(N+1):
if i == 0:
dp[0][j] = j
elif j == 0:
dp[i][0] = i
elif word1[i-1] == word2[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min( dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1
return dp[M][N]
# Note:
# 1. dp[i][j] is Edit Distance of first i-1 chars in word1 with first j-1 chars in word2
# 2. dp[0][j] = j, dp[i][0] = i
# 3. dp[i][j] = dp[i-1][j-1] # if word[i-1] == word[j-1]
# = min( dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) + 1 # if word[i-1] != word[j-1]
# Note:
# 1. This dp is a bit diff, the length of dp is A+1, B+1
# 2. Others are the same, remember how to initiate the dp matrix
# 3. When comparing the i, it compares with word[i-1] and word[j-1]
# This is not hard to think, since we start loop from 1
# 4. Initial value of DP: add N chars for word1
# Transfer function:
# Target somestr1c -> somestr2d
# 1. Assume somestr1 -> somestr2 dp[i][j]
# 2. somestr1 -> somestr2d dp[i-1][j]
# 3. somestr1c -> somestr2 dp[i][j-1]
# 4. i. replace c with d: somestr1 -> somestr2 + 1 : dp[i-1][j-1] + 1
# ii. append d to c : somestr1c -> somestr2 + 1 : dp[i][j-1] + 1
# iii. delete c : somestr1 -> somestr2d + 1 : dp[i-1][j] + 1