每天刷一道牛客题霸-第22天- 最小编辑代价

题目

https://www.nowcoder.com/practice/05fed41805ae4394ab6607d0d745c8e4?tpId=190&&tqId=35213&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;


public class Solution {
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    public static int minEditCost (String str1, String str2, int ic, int dc, int rc) {
        int[][] count = new int[str1.length()+1][str2.length()+1];
        for (int i = 0; i < count.length; i++) {
            count[i][0] = i*dc;
        }
        for (int i = 0; i < count[0].length; i++) {
            count[0][i] = i*ic;
        }
        for (int i = 1; i < count.length; i++) {
            for (int j = 1; j < count[i].length; j++) {
                if (str1.charAt(i-1) == str2.charAt(j-1)) {
                    count[i][j] = count[i-1][j-1];
                }else{
                    count[i][j] =Math.min(count[i][j-1]+ ic,  Math.min(count[i-1][j-1] + rc, count[i-1][j] + dc));
                }
            }
        }
        return count[str1.length()][str2.length()];
        // write code here
    }

}
#牛客题霸##题解#
全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务