forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditDistance.py
More file actions
executable file
·45 lines (39 loc) · 1.34 KB
/
editDistance.py
File metadata and controls
executable file
·45 lines (39 loc) · 1.34 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
""" editDistance
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:
def minDistance(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: int
"""
lgW1, lgW2 = len(word1), len(word2)
dp = [i for i in range(lgW2+1)]
for i in range(1, lgW1+1):
pre, dp[0] = dp[0], i
for j in range(1, lgW2+1):
tmp = dp[j]
dp[j] = pre if word1[i-1] == word2[j-1] else min(pre, dp[j], dp[j-1]) + 1
pre = tmp
return dp[-1]
""" alternative, slower
class Solution:
def minDistance(self, word1, word2):
dist = [[i] for i in range(len(word1)+1)]
dist[0] = [j for j in range(len(word2)+1)]
for i in range(1, len(word1)+1):
for j in range(1, len(word2)+1):
insert = dist[i][j - 1] + 1
delete = dist[i - 1][j] + 1
replace = dist[i - 1][j - 1]
if word1[i-1] != word2[j-1]:
replace += 1
dist[i].append(min(insert, delete, replace))
return dist[-1][-1]
"""