Skip to content

Commit e6b0291

Browse files
committed
Sync LeetCode submission Runtime - 35 ms (90.85%), Memory - 20 MB (71.43%)
1 parent a3b37b2 commit e6b0291

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

0072-edit-distance/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p>
2+
3+
<p>You have the following three operations permitted on a word:</p>
4+
5+
<ul>
6+
<li>Insert a character</li>
7+
<li>Delete a character</li>
8+
<li>Replace a character</li>
9+
</ul>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> word1 = &quot;horse&quot;, word2 = &quot;ros&quot;
16+
<strong>Output:</strong> 3
17+
<strong>Explanation:</strong>
18+
horse -&gt; rorse (replace &#39;h&#39; with &#39;r&#39;)
19+
rorse -&gt; rose (remove &#39;r&#39;)
20+
rose -&gt; ros (remove &#39;e&#39;)
21+
</pre>
22+
23+
<p><strong class="example">Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> word1 = &quot;intention&quot;, word2 = &quot;execution&quot;
27+
<strong>Output:</strong> 5
28+
<strong>Explanation:</strong>
29+
intention -&gt; inention (remove &#39;t&#39;)
30+
inention -&gt; enention (replace &#39;i&#39; with &#39;e&#39;)
31+
enention -&gt; exention (replace &#39;n&#39; with &#39;x&#39;)
32+
exention -&gt; exection (replace &#39;n&#39; with &#39;c&#39;)
33+
exection -&gt; execution (insert &#39;u&#39;)
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>0 &lt;= word1.length, word2.length &lt;= 500</code></li>
41+
<li><code>word1</code> and <code>word2</code> consist of lowercase English letters.</li>
42+
</ul>

0072-edit-distance/solution.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Approach 2: Memoization: Top-Down Dynamic Programming
2+
3+
# M = len(word1), N = len(word2)
4+
# Time: O(M * N)
5+
# Space: O(M * N)
6+
7+
class Solution:
8+
def minDistance(self, word1: str, word2: str) -> int:
9+
memo = [
10+
[None for _ in range(len(word2) + 1)] for _ in range(len(word1) + 1)
11+
]
12+
13+
def minDistanceRecur(word1, word2, word1_idx, word2_idx):
14+
if word1_idx == 0:
15+
return word2_idx
16+
if word2_idx == 0:
17+
return word1_idx
18+
if memo[word1_idx][word2_idx] is not None:
19+
return memo[word1_idx][word2_idx]
20+
21+
minEditDistance = 0
22+
if word1[word1_idx - 1] == word2[word2_idx - 1]:
23+
minEditDistance = minDistanceRecur(word1, word2, word1_idx - 1, word2_idx - 1)
24+
else:
25+
insert_operation = minDistanceRecur(word1, word2, word1_idx, word2_idx - 1)
26+
delete_operation = minDistanceRecur(word1, word2, word1_idx - 1, word2_idx)
27+
replace_operation = minDistanceRecur(word1, word2, word1_idx - 1, word2_idx - 1)
28+
minEditDistance = min(insert_operation, delete_operation, replace_operation) + 1
29+
30+
memo[word1_idx][word2_idx] = minEditDistance
31+
return minEditDistance
32+
33+
return minDistanceRecur(word1, word2, len(word1), len(word2))
34+

0 commit comments

Comments
 (0)