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

计算字符串的距离

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));
}
全部评论

相关推荐

只写bug的程序媛:人家说一本以上,不是及以上
点赞 评论 收藏
分享
评论
11
3
分享

创作者周榜

更多
牛客网
牛客企业服务