Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44474a1

Browse files
add cleaner solution
1 parent a25fff3 commit 44474a1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎Kangli/DP/editDistance.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,28 @@ def minDistance(self, word1, word2):
1313
dp[i][j] = min(dp[i-1][j]+1, dp[i][j-1]+1, dp[i-1][j-1]+(word1[i-1]!=word2[j-1]))
1414
return dp[-1][-1]
1515

16+
#sample 282 ms submission
17+
class Solution(object):
18+
def minDistance(self, word1, word2):
19+
"""
20+
:type word1: str
21+
:type word2: str
22+
:rtype: int
23+
"""
24+
m = len(word1)
25+
n = len(word2)
26+
dp = [[0 for x in xrange(n+1)] for x in xrange(m+1)]
27+
28+
for i in xrange(m+1):
29+
for j in xrange(n+1):
30+
if i == 0:
31+
dp[i][j] = j
32+
elif j == 0:
33+
dp[i][j] = i
34+
elif word1[i-1] == word2[j-1]:
35+
dp[i][j] = dp[i-1][j-1]
36+
else:
37+
dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
38+
39+
return dp[m][n]
1640

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /