当前位置 博文首页 > Inmaturity_7的博客:算法练习帖--65--尽可能使字符串相等(Java)

    Inmaturity_7的博客:算法练习帖--65--尽可能使字符串相等(Java)

    作者:[db:作者] 时间:2021-07-16 21:49

    尽可能使字符串相等

    一、题目简介

    给你两个长度相同的字符串,s 和 t。
    将 s 中的第 i 个字符变到 t 中的第 i 个字符需要 |s[i] - t[i]| 的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。

    用于变更字符串的最大预算是 maxCost。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。

    如果你可以将 s 的子字符串转化为它在 t 中对应的子字符串,则返回可以转化的最大长度。
    如果 s 中没有子字符串可以转化成 t 中对应的子字符串,则返回 0。

    题目来源:力扣(LeetCode)

    示例 1:
    输入:s = "abcd", t = "bcdf", cost = 3
    输出:3
    解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3。
    
    示例 2:
    输入:s = "abcd", t = "cdef", cost = 3
    输出:1
    解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1。
    
    示例 3:
    输入:s = "abcd", t = "acde", cost = 0
    输出:1
    解释:你无法作出任何改动,所以最大长度为 1。
    
    提示:
    1 <= s.length, t.length <= 10^5
    0 <= maxCost <= 10^6
    s 和 t 都只含小写英文字母。
    

    二、解决方法

    1. 前缀和 + 二分查找

    class Solution {
        public int equalSubstring(String s, String t, int maxCost) {
            int n = s.length();
            int[] accDiff = new int[n + 1];//前缀和数组
            for (int i = 0; i < n; i++) {//前缀和数组初始化
                accDiff[i + 1] = accDiff[i] + Math.abs(s.charAt(i) - t.charAt(i));
            }
            int maxLength = 0;//字符串最大长度
            for (int i = 1; i <= n; i++) {
            	//获取在消费maxCost后能够达到的最前下标
                int start = binarySearch(accDiff, i, accDiff[i] - maxCost);
                maxLength = Math.max(maxLength, i - start);//比较后获取最大长度
            }
            return maxLength;
        }
    	/**
    	*二分查找
    	*@param accDiff 前缀和数组
    	*@param endIndex 终点下标
    	*@param target 目标值
    	**/
        public int binarySearch(int[] accDiff, int endIndex, int target) {
            int low = 0, high = endIndex;
            while (low < high) {
                int mid = (high - low) / 2 + low;
                if (accDiff[mid] < target) {
                    low = mid + 1;
                } else {
                    high = mid;
                }
            }
            return low;
        }
    }
    

    2. 滑动窗口(双指针)

    package com.lxf.test2;
    
    public class EqualSubstring {
        public int equalSubstring(String s, String t, int maxCost) {
            char[] sc = s.toCharArray();
            char[] tc = t.toCharArray();
            int left=0;//左指针
            int right=0;//右指针
            int cost=0;//当前花费体力值
            //我们维持一个right-left大小的窗口
            //如果cost<=maxCost,则说明窗口能够扩大,right++.
            //如果cost>maxCost,说明窗口不能扩大,我们需要维持本窗口大小,right++,left--.
            while(right<sc.length){
                cost+=Math.abs(sc[right]-tc[right]);//加上right下标的移动体力值
                if(cost>maxCost){
                    //如果当前体力值大于maxCost,则减去left下标的移动体力值,并left++
                    //这里相当于一次窗口大小不变操作,因为我们记录的永远是当前状态下最大的窗口
                    cost-=Math.abs(sc[left]-tc[left]);
                    left++;
                }
                right++;
            }
            return right-left;
        }
    }
    
    
    cs