📌  相关文章
📜  通过交换成对的相邻字符来检查两个字符串是否相等

📅  最后修改于: 2021-04-17 15:27:59             🧑  作者: Mango

给定两个字符串A和分别的长度NM B和阵列ARR []选自由K的整数,所述的任务是检查字符串B可以从字符串A通过交换任一对字符串A的相邻的字符的获得任何次数,以使交换的索引不存在于数组arr []中。如果可以将字符串A转换为字符串B,则打印“是” 。否则,打印“否”

例子:

方法:按照以下步骤解决问题:

  • 如果两个字符串的长度不相等,则字符串A不能转换为字符串B。因此,打印“否”
  • 遍历给定的数组arr []并检查A [arr [i]]B [arr [i]]字符是否相同。如果发现是真的,则打印“否” 。否则,请执行以下步骤:
    • 如果arr [i]的第一个元素不为0
      • AB的所有字符从0arr [i]存储在两个字符串,例如XY。
      • 求出字符串XY的字符频率。
      • 如果所有字符的频率相同,则打印“ No ”。
    • 同样,如果arr [i]的最后一个元素不等于索引(N – 1)处的元素,则:
      • AB(arr [i] +1)N的所有字符存储在两个字符串,例如XY。
      • 求出字符串XY的字符频率。
      • 如果所有字符的频率相同,则打印“ No ”。
    • 循环从1N,并初始化两个指针L = arr [i – 1]R = arr [i]
      • AB(L +1)R的所有字符存储在两个字符串,例如XY。
      • 求出字符串XY的字符频率。
      • 如果所有字符的频率相同,则打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to count frequency of the
// characters of two given strings
bool isEqual(string& A, string& B)
{
    // Stores the frequencies of
    // characters of strings A and B
    map mp, mp1;
  
    // Traverse the string A
    for (auto it : A) {
  
        // Update frequency of
        // each character
        mp[it]++;
    }
  
    // Traverse the string B
    for (auto it : B) {
  
        // Update frequency of
        // each character
        mp1[it]++;
    }
  
    // Traverse the Map
    for (auto it : mp) {
  
        // If the frequency a character
        // is not the same in both the strings
        if (it.second != mp1[it.first]) {
            return false;
        }
    }
  
    return true;
}
  
// Function to check if it is possible
// to convert string A to string B
void IsPossible(string& A, string& B,
                int arr[], int N)
{
  
    // Base Case
    if (A == B) {
        cout << "Yes" << endl;
        return;
    }
  
    // If length of both the
    // strings are not equal
    if (A.length() != B.length()) {
        cout << "No" << endl;
        return;
    }
  
    // Traverse the array and check
    // if the blocked indices
    // contains the same character
    for (int i = 0; i < N; i++) {
        int idx = arr[i];
  
        // If there is a different
        // character, return No
        if (A[idx] != B[idx]) {
            cout << "No" << endl;
            return;
        }
    }
  
    // If the first index is not present
    if (arr[0]) {
        string X = "";
        string Y = "";
  
        // Extract characters from
        // string A and B
        for (int i = 0; i < arr[0]; i++) {
            X += A[i];
            Y += B[i];
        }
  
        // If frequency is not same
        if (!isEqual(A, B)) {
  
            cout << "No" << endl;
            return;
        }
    }
  
    // If last index is not present
    if (arr[N - 1] != (A.length() - 1)) {
        string X = "";
        string Y = "";
  
        // Extract characters from
        // string A and B
        for (int i = arr[N - 1] + 1;
             i < A.length(); i++) {
            X += A[i];
            Y += B[i];
        }
  
        // If frequency is not same
        if (!isEqual(A, B)) {
            cout << "No" << endl;
            return;
        }
    }
  
    // Traverse the array arr[]
    for (int i = 1; i < N; i++) {
  
        int L = arr[i - 1];
        int R = arr[i];
  
        string X = "";
        string Y = "";
  
        // Extract characters from strings A and B
        for (int j = L + 1; j < R; j++) {
            X += A[j];
            Y += B[j];
        }
  
        // If frequency is not same
        if (!isEqual(A, B)) {
            cout << "No" << endl;
            return;
        }
    }
  
    // If all conditions are satisfied
    cout << "Yes" << endl;
}
  
// Driver Code
int main()
{
    string A = "abcabka";
    string B = "acbakba";
    int arr[] = { 0, 3, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    IsPossible(A, B, arr, N);
  
    return 0;
}


输出:
Yes

时间复杂度: O(N)
辅助空间: O(N)