📜  成本小于 K 的最长相等子串

📅  最后修改于: 2022-05-13 01:57:08.197000             🧑  作者: Mango

成本小于 K 的最长相等子串

给定两个长度相同的字符串XY ,它们由小写字母和整数K组成。任务是找到在给定成本KX可以更改为Y的最大长度。
更改字符的成本由字符的 ASCII 值之间的绝对差值给出。也就是说,要更改索引i处的字符,cost = |x[i] – Y[i]|

例子:

方法:想法是维护一个长度为 N 的前缀数组来存储字符串的绝对和。也就是说,将字符串X更改为Y的成本。可以按照以下步骤计算结果:

  • 维护两个指针,比如ij
  • 在while循环中,检查前缀数组的第i索引和第j索引之间的差异是否大于给定的成本。
  • 如果差值大于给定成本,则增加 j 指针以补偿成本,否则增加 i 指针。

下面是上述方法的实现:

C++
// C++ program to find the
// maximum length of equal substring
// within a given cost
#include 
using namespace std;
 
// Function to find the maximum length
int solve(string X, string Y, int N, int K)
{
 
    int count[N + 1] = { 0 };
    int sol = 0;
    count[0] = 0;
 
    // Fill the prefix array with
    // the difference of letters
    for (int i = 1; i <= N; i++) {
 
        count[i] = count[i - 1] + abs(X[i - 1] - Y[i - 1]);
    }
 
    int j = 0;
 
    for (int i = 1; i <= N; i++) {
        while ((count[i] - count[j]) > K) {
 
            j++;
        }
 
        // Update the maximum length
        sol = max(sol, i - j);
    }
 
    return sol;
}
 
// Driver code
int main()
{
 
    int N = 4;
 
    string X = "abcd", Y = "bcde";
 
    int K = 3;
 
    cout << solve(X, Y, N, K) << "\n";
 
    return 0;
}


Java
// Java program to find the
// maximum length of equal subString
// within a given cost
class GFG
{
 
// Function to find the maximum length
static int solve(String X, String Y,
                int N, int K)
{
 
    int []count = new int[N + 1];
    int sol = 0;
    count[0] = 0;
 
    // Fill the prefix array with
    // the difference of letters
    for (int i = 1; i <= N; i++)
    {
 
        count[i] = count[i - 1] +
                Math.abs(X.charAt(i - 1) -
                Y.charAt(i - 1));
    }
 
    int j = 0;
 
    for (int i = 1; i <= N; i++)
    {
        while ((count[i] - count[j]) > K)
        {
            j++;
        }
 
        // Update the maximum length
        sol = Math.max(sol, i - j);
    }
 
    return sol;
}
 
// Driver code
public static void main(String[] args)
{
 
    int N = 4;
    String X = "abcd", Y = "bcde";
    int K = 3;
 
    System.out.print(solve(X, Y, N, K) + "\n");
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to find the
# maximum length of equal subString
# within a given cost
 
# Function to find the maximum length
def solve(X, Y, N, K):
    count = [0] * (N + 1);
    sol = 0;
    count[0] = 0;
 
    # Fill the prefix array with
    # the difference of letters
    for i in range(1, N + 1):
        count[i] = (count[i - 1] +
                    abs(ord(X[i - 1]) -
                    ord(Y[i - 1])));
 
    j = 0;
 
    for i in range(1, N + 1):
        while ((count[i] - count[j]) > K):
            j += 1;
 
        # Update the maximum length
        sol = max(sol, i - j);
 
    return sol;
 
# Driver code
if __name__ == '__main__':
    N = 4;
    X = "abcd";
    Y = "bcde";
    K = 3;
 
    print(solve(X, Y, N, K));
 
# This code is contributed by PrinciRaj1992


C#
// C# program to find the
// maximum length of equal subString
// within a given cost
using System;
 
class GFG
{
     
    // Function to find the maximum length
    static int solve(string X, string Y,
                    int N, int K)
    {
     
        int []count = new int[N + 1];
        int sol = 0;
        count[0] = 0;
     
        // Fill the prefix array with
        // the difference of letters
        for (int i = 1; i <= N; i++)
        {
     
            count[i] = count[i - 1] +
                    Math.Abs(X[i - 1] -
                    Y[i - 1]);
        }
     
        int j = 0;
     
        for (int i = 1; i <= N; i++)
        {
            while ((count[i] - count[j]) > K)
            {
                j++;
            }
     
            // Update the maximum length
            sol = Math.Max(sol, i - j);
        }
     
        return sol;
    }
 
    // Driver code
    public static void Main()
    {
     
        int N = 4;
        string X = "abcd", Y = "bcde";
        int K = 3;
     
        Console.WriteLine(solve(X, Y, N, K) + "\n");
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
3