Skip to content

Commit

Permalink
day31
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-KOUAGOU committed May 31, 2020
1 parent 9b12da1 commit a84c340
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions MayChallenge_LeetCode.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,36 @@
" return sorted(points, key=distance)[0:K]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Edit Distance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Solution:\n",
" def minDistance(self, word1: str, word2: str) -> int:\n",
" word1, word2 = \"!\" + word1, \"!\" + word2\n",
" n_1, n_2 = len(word1), len(word2)\n",
" dp = [[0] * n_2 for _ in range(n_1)]\n",
"\n",
" for i in range(n_1): dp[i][0] = i\n",
" for j in range(n_2): dp[0][j] = j\n",
"\n",
" for i in range(1, n_1):\n",
" for j in range(1,n_2):\n",
" Cost = (word1[i] != word2[j])\n",
" dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + Cost)\n",
"\n",
" return int(dp[-1][-1]) "
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit a84c340

Please sign in to comment.