[LeetCode] 208. Get Equal Substrings Within Budget (C++)


Source of subject: https://leetcode-cn.com/problems/get-equal-substrings-within-budget/

Title description

Give you two strings of the same length, s and t.
Changing the i-th character in s to the i-th character in t requires an overhead of |s[i]-t[i]| (the overhead may be 0), which is the difference between the ASCII code values ​​of the two characters Absolute value.

The maximum budget for changing strings is maxCost. When converting strings, the total cost should be less than or equal to the budget, which also means that the conversion of strings may be incomplete.

If you can convert the substring of s into its corresponding substring in t, return the maximum length that can be converted.

If there is no substring in s that can be converted into the corresponding substring in t, 0 is returned.

示例 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 都只含小写英文字母。

General idea

  • Given two strings, turn the characters in the string into the characters at the corresponding positions in t (the absolute value of the distance between the characters), and given a cost, find the length of the longest substring that can be converted at most

Sliding window + dual pointer

  • The distance between characters is stored in a dif array, and the right window of the sliding window is used as the loop variable. If the current character needs to jump, the current spent count is added up. If it is greater than the maxCount at this time, it will gradually Move the left window, and update the longest window length at this time
const int MAXN = 1e5 + 1;
int diff[MAXN];
class Solution {
    
    
public:
    int equalSubstring(string s, string t, int maxCost) {
    
    
        int left = 0, right = 0, ans = 0;
        int len = s.length();
        memset(diff, 0, sizeof(diff));
        for(int i = 0 ; i < len ; ++i)  diff[i] = abs(s[i] - t[i]);
        int curCost = 0;
        for(; right < len ; ++right){
    
    
            curCost += diff[right];
            while(curCost > maxCost){
    
    
                curCost -= diff[left];
                ++left;
            }
            ans = max(ans, right - left + 1);
        }
        return ans;
    }
};

Complexity analysis

  • Time complexity: O(n). n is the length of the array, both left and right can be regarded as sliding n times
  • Space complexity: O(n). n is the length of the array

Prefix and + binary search

  • Because the array of distances between characters stored here are all positive numbers, this inspired us to use the idea of ​​prefix sum to generate a monotonically increasing sequence, so that binary search can be performed directly, O(logn) finds the last one that satisfies the following conditions Number, and update the longest window length
    sums [bound] − bound [right − 1] >= target sums[bound]-bound[right-1] >=targetsums[bound]bound[right1]>=target
class Solution {
    
    
public:
    int equalSubstring(string s, string t, int maxCost) {
    
    
        int left = 0, ans = 0, len = s.length();
        int nums[len + 1];
        memset(nums, 0, sizeof(nums));
        for(int i = 1 ; i <= len ; ++i)  nums[i] = nums[i - 1] + abs(s[i - 1] - t[i - 1]);
        for(int right = 1; right <= len ; ++right){
    
    
            int target = nums[right - 1] + maxCost;
            int index = BinarySearch(target, nums, 0, len);
            ans = max(ans, index - right + 1);
        }
        return ans;
    }
    int BinarySearch(int target, int* nums,int left, int right){
    
    
        int len = right;
        while(left <= right){
    
    
            int mid = right + ((left - right) >> 1);
            if(nums[mid] > target)  right = mid - 1;
            else{
    
    
                if(mid == len || nums[mid + 1]  > target) return mid;
                else left = mid + 1;
            }
        }
        return -1;
    }
};

Complexity analysis

  • Time complexity: O(nlogn). n is the length of the array, n cycles * logn is the time complexity of binary search
  • Space complexity: O(n). n is the length of the array

Guess you like

Origin blog.csdn.net/lr_shadow/article/details/114000522