Likou 1208. Make the strings as equal as possible

topic

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 not be complete.
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.

Example 1:

Input: s = "abcd", t = "bcdf", cost = 3
Output: 3
Explanation: "abc" in s can be changed to "bcd". The cost is 3, so the maximum length is 3.

Example 2:

Input: s = "abcd", t = "cdef", cost = 3
Output: 1
Explanation: If any character in s wants to become the corresponding character in t, the cost is 2. Therefore, the maximum length is 1.

Example 3:

Input: s = "abcd", t = "acde", cost = 0
Output: 1
Explanation: You cannot make any changes, so the maximum length is 1.

prompt:

1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s 和 t 都只含小写英文字母。

Source: LeetCode
Link: https://leetcode-cn.com/problems/get-equal-substrings-within-budget
Template:

int equalSubstring(char * s, char * t, int maxCost){
    
    

}

First look at the variables in the template

st is the two character arrays before and after conversion
maxcost is the maximum conversion budget

The entire execution process is as follows:

flow chart

int equalSubstring(char* s, char* t, int maxCost) {
    
    
    int n = strlen(s);//获取字符串s的长度
    int diff[n];//建立长度与s字符串长度一致的整型数组diff
    memset(diff, 0, sizeof(diff));//将diff用0注满
    for (int i = 0; i < n; i++) {
    
    
        diff[i] = fabs(s[i] - t[i]);//将第i个元素转化所需的开销存入diff的第i个位置
    }
    int maxLength = 0;
    int start = 0, end = 0;
    int sum = 0;
    while (end < n) {
    
    //当终点指针仍在diff长度范围内
        sum += diff[end];//将此时end所在的元素的所需的开销加到总计数里
        while (sum > maxCost) {
    
    //若此时总开销超出开销限制执行
            sum -= diff[start];//从总开销中移除start所指的开销
            start++;//start指针后移
        }
        maxLength = fmax(maxLength, end - start + 1);//最大长度获取更新
        end++;//end指针位置后移
    }
    return maxLength;
}

The above is the idea of ​​the official code, but there are still many references to some basic functions, such as:
mmset();fabs();fmax();

And talk about my personal understanding:
Insert picture description here
here are two while loops, do the following operations:
1. Starting from the first element as start, the unit that can be converted to the maximum cost as end, this stage will continue Update the maximum conversion length;
2. When the cost of the first element as start and known end has exceeded the expected cost, move start to the position of the second element, and continue to perform end displacement;
3. Repeatedly Make the maximum conversion length in the array get and return.
Note: It is not necessary to parse all the subset arrays for all elements, because what we need is the longest transformable array length. After each new length is obtained, we will no longer consider the following lower than the current length The sub-array is out.Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44922487/article/details/113677209