题解 | #计算字符串的距离#

计算字符串的距离

http://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314

var minDistance = function(word1, word2) {
    let n = word1.length;
    let m = word2.length;
    if(n*m === 0) return n+m;
    if(word1 === word2) return 0;
    let dp = [];
    for(let i = 0;i <= n;i++){
        dp.push([])
        for(let j = 0;j <= m;j++){
            if(i*j){
                dp[i][j] = word1[i-1] == word2[j-1]? dp[i-1][j-1]: (Math.min(dp[i-1][j],dp[i][j-1],dp[i-1][j-1]) + 1);
            }else{
                dp[i][j] = i + j;
            }
        }
    }
    return dp[n][m];
};



while(str1 = readline()) {
  const str2 = readline();
  console.log(minDistance(str1, str2));
}
全部评论

相关推荐

能干的三文鱼刷了10...:公司可能有弄嵌入式需要会画pcb的需求,而且pcb能快速直观看出一个人某方面的实力。看看是否有面试资格。问你问题也能ai出来,pcb这东西能作假概率不高
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
评论
11
4
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务