📌  相关文章
📜  使用追加和删除最后操作将一个字符串转换为另一个字符串

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

使用追加和删除最后操作将一个字符串转换为另一个字符串

给定一个整数 k 和两个字符串str1 和 str2 确定我们是否可以通过对 str1 执行以下恰好 k 个操作来将 str1 转换为 str2。
a) 在 str1 的末尾附加一个小写英文字母。
b) 删除 str1 中的最后一个字符(对空字符串执行此操作会导致空字符串)
例子:

Input : k = 7, str1 = aba, str2 = aba
Output : Yes
(4 operations to convert str1 to an 
empty string(to make string empty we 
have to perform one more delete 
operation) and 3 append operations)

Input : k = 5, str1 = pqruvs, str2 = pqrxy 
Output : Yes
(3 delete operations and 2 append operations)

首先,我们确定两个字符串的公共前缀,然后根据公共前缀的值 str1.length、str2.length 和 k 我们可以得出结果。以下是案例。
案例 A:我们可以将 str1 更改为 str2 的情况:

  • 如果 str1.length + str2.length <= k 那么我们可以完全删除 str1 并轻松地重新构造 str2。
  • 如果[k-(str1.length-prefix.length + str2.length-prefix.length)]是偶数,那么我们可以很容易地从 str1 构造 str2。这是因为 str1.length-prefix.length 是要删除的字母数,而 str2.length-prefix.length 是删除不常见字母后要在 str1 中添加的字母数。在此之后,如果剩下的操作是偶数,那么我们可以添加和删除任何花费偶数操作的随机字母。

案例 B:在其余所有情况下,我们不能从 str1 构造 str2。

C++
// CPP Program to convert str1 to str2 in
// exactly k operations
#include 
using namespace std;
 
// Returns true if it is possible to convert
// str1 to str2 using k operations.
bool isConvertible(string str1, string str2,
                                      int k)
{
    // Case A (i)
    if ((str1.length() + str2.length()) < k)
        return true;
 
    // finding common length of both string
    int commonLength = 0;
    for (int i = 0; i < min(str1.length(),
                           str2.length()); i++) {
        if (str1[i] == str2[i])
            commonLength++;
        else
            break;
    }
 
    // Case A (ii)-
    if ((k - str1.length() - str2.length() +
                     2 * commonLength) % 2 == 0)
        return true;
 
    // Case B-
    return false;
}
 
// driver program
int main()
{
    string str1 = "geek", str2 = "geek";
    int k = 7;
    if (isConvertible(str1, str2, k))
       cout << "Yes";
    else
       cout << "No";
 
    str1 = "geeks",  str2 = "geek";
    k = 5;   
    cout << endl;
    if (isConvertible(str1, str2, k))
       cout << "Yes";
    else
       cout << "No";
    return 0;
}


Java
// java Program to convert str1 to
// str2 in exactly k operations
import java.io.*;
 
class GFG {
     
    // Returns true if it is possible to convert
    // str1 to str2 using k operations.
    static boolean isConvertible(String str1, String str2,
                                                     int k)
    {
        // Case A (i)
        if ((str1.length() + str2.length()) < k)
            return true;
     
        // finding common length of both string
        int commonLength = 0;
        for (int i = 0; i < Math.min(str1.length(),
                                str2.length()); i++)
        {
            if (str1 == str2)
                commonLength++;
            else
                break;
        }
     
        // Case A (ii)-
        if ((k - str1.length() - str2.length() +
                     2 * commonLength) % 2 == 0)
            return true;
     
        // Case B
        return false;
    }
     
     
     
    // Driver program
    public static void main (String[] args)
    {
        String str1 = "geek";
        String str2 = "geek";
        int k = 7;
        if (isConvertible(str1, str2, k))
        System.out.println( "Yes");
        else
        System.out.println ( "No");
     
        str1 = "geeks";
        str2 = "geek";
        k = 5;
         
        if (isConvertible(str1, str2, k))
        System.out.println( "Yes");
        else
        System.out.println ( "No");
             
    }
}
 
// This code is contributed by vt_m.


Python3
# Python 3 Program to convert str1 to
# str2 in exactly k operations
 
# Returns true if it is possible to convert
# str1 to str2 using k operations.
def isConvertible(str1, str2, k):
     
    # Case A (i)
    if ((len(str1) + len(str2)) < k):
        return True
 
    # finding common length of both string
    commonLength = 0
    for i in range(0, min(len(str1),
                          len(str2)), 1):
        if (str1[i] == str2[i]):
            commonLength += 1
        else:
            break
 
    # Case A (ii)-
    if ((k - len(str1) - len(str2) + 2 *
                commonLength) % 2 == 0):
        return True
 
    # Case B-
    return False
 
# Driver Code
if __name__ == '__main__':
    str1 = "geek"
    str2 = "geek"
    k = 7
    if (isConvertible(str1, str2, k)):
        print("Yes")
    else:
        print("No")
 
    str1 = "geeks"
    str2 = "geek"
    k = 5
    if (isConvertible(str1, str2, k)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by
# Sanjit_Prasad


C#
// C# Program to convert str1 to
// str2 in exactly k operations
using System;
 
class GFG {
     
    // Returns true if it is possible to convert
    // str1 to str2 using k operations.
    static bool isConvertible(string str1, string str2,
                                                    int k)
    {
        // Case A (i)
        if ((str1.Length + str2.Length) < k)
            return true;
     
        // finding common length of both string
        int commonLength = 0;
        for (int i = 0; i < Math.Min(str1.Length,
                                str2.Length); i++)
        {
            if (str1 == str2)
                commonLength++;
            else
                break;
        }
     
        // Case A (ii)-
        if ((k - str1.Length - str2.Length +
                    2 * commonLength) % 2 == 0)
            return true;
     
        // Case B
        return false;
    }
     
     
     
    // Driver program
    public static void Main ()
    {
        string str1 = "geek";
        string str2 = "geek";
        int k = 7;
        if (isConvertible(str1, str2, k))
        Console.WriteLine( "Yes");
        else
        Console.WriteLine ( "No");
     
        str1 = "geeks";
        str2 = "geek";
        k = 5;
         
        if (isConvertible(str1, str2, k))
        Console.WriteLine( "Yes");
        else
        Console.WriteLine ( "No");
             
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:
No
Yes