LeetCode 72 - Edit Distance
Difficulty: hard
Problem Description
English (Edit Distance)
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
1 | |
Example 2:
1 | |
Constraints:
- 0 <= word1.length, word2.length <= 500
word1andword2consist of lowercase English letters.
Chinese (编辑距离)
给你两个单词 word1 和 word2,请返回 将 word1 转换成 word2 所使用的最少操作数。
你可以对一个单词进行如下三种操作:
- 插入一个字符
- 删除一个字符
- 替换一个字符
示例 1:
1 | |
示例 2:
1 | |
提示:
0 <= word1.length, word2.length <= 500word1和word2由小写英文字母组成
Solution
C++
1 | |
Python
1 | |
Advanced Strategy for Less Space Occupied
It’s available to compress the dp array into one dimension.
Advanced: Count the Number of Times Each Operation Takes
Solution for counting the number of times each operation takes
1 | |
Test Cases
1 | |
LeetCode 72 - Edit Distance
http://wasprime.github.io/Algorithm/LeetCode/DynamicProgramming/LeetCode-72-Edit-Distance/