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
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 079e846

Browse files
finished solution for problem66
1 parent e390e68 commit 079e846

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

‎problem66/Solution.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
class Solution:
22
def minDistance(self, word1: str, word2: str) -> int:
3+
cache = [[float('inf')] * (len(word2)+1) for _ in range(len(word1) +1)]
4+
5+
for i in range(len(cache)):
6+
cache[i][len(word2)] = len(word1) - i
37

8+
for j in range(len(cache[0])):
9+
cache[len(word1)][j] = len(word2) - j
10+
11+
for i in range(len(word1) -1, -1, -1):
12+
for j in range(len(word2) -1, -1, -1):
13+
if word1[i] == word2[j]:
14+
cache[i][j] = cache[i+1][j+1]
15+
else:
16+
cache[i][j] = 1 + min(cache[i][j+1], cache[i+1][j], cache[i+1][j+1])
17+
18+
return cache[0][0]

0 commit comments

Comments
(0)

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