📌  相关文章
📜  通过将 A[i] 更改为 A[i+1] 或 A[i]..A[i+K-1] 分别更改为 A[i]+1 来检查字符串A 是否可以转换为字符串B

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

通过将 A[i] 更改为 A[i+1] 或 A[i]..A[i+K-1] 分别更改为 A[i]+1 来检查字符串A 是否可以转换为字符串B

给定两个长度为N的字符串AB和一个整数K ,任务是找出字符串A是否可以转换为字符串B ,使用以下操作任意次数:

  • Type1 : 选择索引i ,并交换A iA i+1
  • Type2 : 选择索引i ,如果A i, A i+1 , ..., A i+K-1都等于某个字符ch ( ch ≠ z ) ,则将每个字符替换为其下一个字符(ch+1) ,例如:“d”被“e”替换,依此类推。

例子:

方法:观察type1的操作,很明显在一些有限的交换序列之后,字符串可以以任何方式重新排序。所以在第二种类型的操作过程中不需要担心字符相邻(因为字符串的重新排序可以随时进行),所以只有字符的频率很重要。以下是要遵循的步骤:

  • 因此,要将字符串A转换为字符串B ,需要使字母表中每个字符的频率相等,然后使用first type的操作对字符串进行重新排序。
  • 如果对于任何字符i ,出现次数不足(频率i,A < 频率i,B )或者如果剩余的字符出现不能转换为下一个字符(频率i,A - 频率i,B )不是K的倍数(因为需要将字符转换为大于K的下一个字符长度),则答案将为NO否则为YES

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
void solve(string& A, string& B, int& N,
           int& K)
{
    // Initializing two vectors to count
    // frequency of two strings
    int arr1[26] = { 0 }, arr2[26] = { 0 };
    bool flag = true;
    for (int i = 0; i < N; i++) {
        arr1[A[i] - 'a']++;
        arr2[B[i] - 'a']++;
    }
    int count = 0;
    for (int i = 0; i < 26 && flag; i++) {
 
        // Till 'y' is not encountered,
        // say ch would be changed to ch+1
        arr1[i] += count;
 
        // Doing required operation to check
        // if frequencies of each character
        // of alphabet is atleast equal
        if (arr1[i] >= arr2[i]) {
 
            // Checking for case when
            // remaining occurrences cannot be
            // converted to next character
            if ((arr1[i] - arr2[i]) % K) {
                flag = false;
            }
            // Here, the characters from
            // string A which were needed
            // for string B are taken in count
            count = arr1[i] - arr2[i];
            continue;
        }
        else {
            flag = false;
        }
    }
    if (flag) {
        cout << "Yes"
             << "\n";
    }
    else {
        cout << "No"
             << "\n";
    }
}
 
// Driver Code
int main()
{
    string A = "zz", B = "aa";
    int N = A.size();
    int K = 1;
    solve(A, B, N, K);
    return 0;
}


Java
// Java program for the above approach
class GFG {
 
  static void solve(String A, String B, int N, int K)
  {
 
    // Initializing two vectors to count
    // frequency of two Strings
    int[] arr1 = new int[26];
    int[] arr2 = new int[26];
    for (int i = 0; i < 26; i++) {
      arr1[i] = 0;
      arr2[i] = 0;
    }
 
    boolean flag = true;
    for (int i = 0; i < N; i++) {
      arr1[A.charAt(i) - 'a']++;
      arr2[B.charAt(i) - 'a']++;
    }
    int count = 0;
    for (int i = 0; i < 26 && flag; i++) {
 
      // Till 'y' is not encountered,
      // say ch would be changed to ch+1
      arr1[i] += count;
 
      // Doing required operation to check
      // if frequencies of each character
      // of alphabet is atleast equal
      if (arr1[i] >= arr2[i]) {
 
        // Checking for case when
        // remaining occurences cannot be
        // converted to next character
        if ((arr1[i] - arr2[i]) % K == 1) {
          flag = false;
        }
        // Here, the characters from
        // String A which were needed
        // for String B are taken in count
        count = arr1[i] - arr2[i];
        continue;
      }
      else {
        flag = false;
      }
    }
    if (flag) {
      System.out.println("Yes");
    }
    else {
      System.out.println("No");
    }
  }
 
  // Driver Code
  public static void main(String args[])
  {
    String A = "zz", B = "aa";
    int N = A.length();
    int K = 1;
    solve(A, B, N, K);
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# Python code for the above approach
def solve(A, B, N, K):
 
    # Initializing two vectors to count
    # frequency of two strings
    arr1 = [0] * 26
    arr2 = [0] * 26
    flag = True;
    for i in range(N):
        arr1[ord(A[i]) - ord('a')] += 1
        arr2[ord(B[i]) - ord('a')] += 1
     
    count = 0;
    for i in range(26):
        if(flag):
           
            # Till 'y' is not encountered,
            # say ch would be changed to ch+1
            arr1[i] += count;
 
            # Doing required operation to check
            # if frequencies of each character
            # of alphabet is atleast equal
            if (arr1[i] >= arr2[i]):
 
                # Checking for case when
                # remaining occurences cannot be
                # converted to next character
                if ((arr1[i] - arr2[i]) % K):
                    flag = False;
                 
                # Here, the characters from
                # string A which were needed
                # for string B are taken in count
                count = arr1[i] - arr2[i];
                continue;
         
            else:
                flag = False;
         
    if (flag):
        print("Yes")
    else:
        print("No")
     
# Driver Code
A = "zz"
B = "aa";
N = len(A)
K = 1;
solve(A, B, N, K);
 
# This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG {
 
  static void solve(string A, string B, int N, int K)
  {
    // Initializing two vectors to count
    // frequency of two strings
    int[] arr1 = new int[26];
    int[] arr2 = new int[26];
    for (int i = 0; i < 26; i++) {
      arr1[i] = 0;
      arr2[i] = 0;
    }
 
    bool flag = true;
    for (int i = 0; i < N; i++) {
      arr1[A[i] - 'a']++;
      arr2[B[i] - 'a']++;
    }
    int count = 0;
    for (int i = 0; i < 26 && flag; i++) {
 
      // Till 'y' is not encountered,
      // say ch would be changed to ch+1
      arr1[i] += count;
 
      // Doing required operation to check
      // if frequencies of each character
      // of alphabet is atleast equal
      if (arr1[i] >= arr2[i]) {
 
        // Checking for case when
        // remaining occurences cannot be
        // converted to next character
        if ((arr1[i] - arr2[i]) % K == 1) {
          flag = false;
        }
        // Here, the characters from
        // string A which were needed
        // for string B are taken in count
        count = arr1[i] - arr2[i];
        continue;
      }
      else {
        flag = false;
      }
    }
    if (flag) {
      Console.WriteLine("Yes");
    }
    else {
      Console.WriteLine("No");
    }
  }
 
  // Driver Code
  public static void Main()
  {
    string A = "zz", B = "aa";
    int N = A.Length;
    int K = 1;
    solve(A, B, N, K);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
No

时间复杂度: 在)
辅助空间: 在)