Edit Distance
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
Example:
动态规划 时间复杂度 O(n^2) 空间间复杂度 O(n^2)
很明显的动态规划的题目,问题就在于需要分类讨论。具体的讨论情况在code的注释部分有详细解释。
1 | class Solution: |
动态规划 时间复杂度 O(n^2) 空间间复杂度 O(n)
1 | class Solution: |