编辑距离 Edit Distance(difficult)(Dynamic Programming)

Problem:
Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

You have the following 3 operations permitted on a word:

Insert a character
Delete a character
Replace a character

来源:力扣(LeetCode)

动态规划:
每一步我们可以做的有三种,分别是增、删、改。
数据结构:dp[][]数组
若 word1[i] == word2[j], dp[i][j] =dp[i-1][j-1];
否则 dp[i][j] = min(dp[i-1][j],dp[i-1][j],dp[i-1][j-1])+1;
dp[i-1][j]: 表示对word1第i个元素删除。
dp[i][j-1]: 表示对word1第i个元素后增加。
dp[i-1][j-1]: 表示对word1第i个元素进行替换。

1、初始化dp数组。
2、自底向上或自顶向下解决问题。
最后返回dp数组右下角值。

你可能感兴趣的:(LeetCode,算法,动态规划)