描述
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
分析
编辑距离
是一个很重要的文本相似度度量标准,比较经典,通常的解法都是二维动态规划。dp[i][j]
代表word1中前i个字符和word2前j个字符的编辑距离,则有
dp[i][j] = dp[i - 1][j - 1] (if word1[i] == word2[j])
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1 (otherwise)
代码
Python
1 | class Solution(object): |