华为OD机试真题 - 字符串比较

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i = 0;
        String[] strs = new String[3];
        while (in.hasNextLine()) {
            strs[i] = in.nextLine();
            i++;
        }
        int target = Integer.parseInt(strs[2]);
        System.out.println(getResult(strs[0], strs[1], target));

    }

    public static int getResult(String str1, String str2, int target) {
        int max = 0;

        for (int i = 0; i < str1.length();) {
            int temp = 0;
            int sum = 0;
            for (int j = i; j < str1.length(); j++) {
                int a1 = str1.charAt(j);
                int a2 = str2.charAt(j);
                sum += Math.abs(a2 - a1);
                if (sum <= target)
                    temp++;
                else {
                    break;
                }
            }
            max = Math.max(max, temp);
            i += temp + 1;
        }
        return max;
    }华为OD机试真题 - 字符串比较
全部评论

相关推荐

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