[LeetCode#72]Edit Distance
2015-08-27 12:43
218 查看
Problem:
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
Analysis:
Solution:
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
Analysis:
This problem is very ticky and easy!!! Reference: https://en.wikipedia.org/wiki/Edit_distance For String problem, if we were asked to compare to string according to order, this problem could and should always be efficiently sovled by dynamic programming method. If the problem allow the string to be permutated, we should take care, since it usually should be solved through HashMap way. Idea: Assume you have a dist array, dist[i+1][j+1] means the distance between word1[0, i] and word2[0, j]. You should try to use the proper transitional function to reach the answer dist[word1.length][word2.length]. Note: for this problem dist[0][0] is for empty string "" "". Transitional functions: 1. iff word1[i] == word2[j], dist[i+1][j+1] = dist[i][j]. if (word1.charAt(i-1) == word2.charAt(j-1)) { dist[i][j] = dist[i-1][j-1]; } 2. iff word1[i] != word2[j], the following ways could be used: 2.1 subsitute word1[i] with the same character of word2[j], dist[i+1][j+1] = dist[i][j] + 1. 2.2 add word1[i] into word2, dist[i+1][j+1] = dist[i][j+1] + 1(word1[i] == added) 2.3 add word1[j] into word2, the same as above. if (word1.charAt(i-1) == word2.charAt(j-1)) { ... } else{ int sub = dist[i-1][j-1] + 1; int add_a = dist[i-1][j] + 1; int add_b = dist[i][j-1] + 1; dist[i][j] = Math.min(sub, Math.min(add_a, add_b)); } Skills: 1. Why don't we consider the method of delete? Delete a character from one string is equal to add a character into one string, both of them need one extra operation. To add a character into a string, To delete a word from word2, dist[i][j] = dist[i][j-1] + 1; Thus we have no need to consider the case of deleting a character. 2. Since the transitional function use dist[i-1][j-1], dist[i-1][j] and dist[i][j-1], to avoid corner cases, we should take advatage of empty string. (which help to keep invariance right and code simple!). But you should take care the change in indexing. the i is equeal to i-1 in word's indexing system. int[][] dist = new int[m+1][n+1]; for (int i = 0; i <= m; i++) dist[i][0] = i; for (int j = 0; j <= n; j++) dist[0][j] = j; 0 | 1 2 3 . . . --- |------------ 1 | 2 | 3 | . | . | Key: you must initate the right value for additional row and column.
Solution:
public class Solution { public int minDistance(String word1, String word2) { if (word1 == null || word2 == null) return 0; int m = word1.length(); int n = word2.length(); int[][] dist = new int[m+1][n+1]; for (int i = 0; i <= m; i++) dist[i][0] = i; for (int j = 0; j <= n; j++) dist[0][j] = j; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (word1.charAt(i-1) == word2.charAt(j-1)) { dist[i][j] = dist[i-1][j-1]; } else{ int sub = dist[i-1][j-1] + 1; int add_a = dist[i-1][j] + 1; int add_b = dist[i][j-1] + 1; dist[i][j] = Math.min(sub, Math.min(add_a, add_b)); } } } return dist[m] ; } }
相关文章推荐
- Android px pt dp sp...
- c++中的字符串与数字相互转换
- 删除用户的时候,产生“数据库主体在该数据库中拥有架构,无法删除”的解决办法
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-02-基于Python的卷积运算
- RedisUtil
- 项目源代码目录结构建议
- 张益唐:我的数学人生
- 使用#pragma阻止一些warnings
- 安装及使用YUICompressor(压缩JS,CSS文件)4
- WinForm实现窗体最大化并遮盖任务栏的方法
- Robot Framework 教程 (6) - 使用条件表达式
- 解决cocos2d-x 安卓锁屏再解锁,OPenGL报错;后台转前台异常问题解决办法 (续)
- SDN相关组织之ODL(opendaylight)
- 文本检查点
- WAMP本地搭建Disuz遇到的问题 自用备忘
- iptables 使用
- [分布式java]基于JavaAPI实现消息方式的系统间通信:TCP/IP+NIO
- 3-07. 求前缀表达式的值(25)
- 骨头汤,猪肉汤
- 内核启动分析(三)——zImage 解压缩阶段