Leetcode: Edit Distance
2015-11-02 10:27
330 查看
Question
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
Hide Tags Dynamic Programming String
Hide Similar Problems (M) One Edit Distance
Solution
[code]class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """ l1, l2 = len(word1), len(word2) # res[i][j] means res for the first i elems in word1, and the first j elems in word2 dp, backup = range(l1+1), [0]*(l1+1) for s2ind in range(1,l2+1): backup[0] = s2ind for s1ind in range(1,l1+1): if word1[s1ind-1]==word2[s2ind-1]: backup[s1ind] = dp[s1ind-1] else: backup[s1ind] = min( backup[s1ind-1], dp[s1ind-1], dp[s1ind]) + 1 dp = backup[:] return dp[-1]
相关文章推荐
- java Process的waitFor()
- visual studio 一直显示正在准备解决方案
- normalize.css
- OKHttpClient
- ionic 动态组件(二)
- eclipse创建android项目,无法正常预览布局文件,出现This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in...
- CentOS Mini 安装图像界面
- JavaScript的IIFE(即时执行方法)
- RCP 文件路径问题
- 解决XAMPP与VMware虚拟机进程冲突一劳永逸的方法
- hdu 1394 Minimum Inversion Number(树状数组)
- 山区建小学
- hdu 5527 Too Rich(贪心)
- Myeclipse中maven工程使用jetty插件启动jetty服务器调试
- Django1.7开发博客
- 百度Android地图SDK地理编码及标注
- Documentation/filesystems/fiemap.txt 翻译学习 -- 需完善
- Cocos2d-x多场景切换生命周期
- string字符串得到生日
- phpstorm的主题相关