📌  相关文章
📜  通过执行交换操作检查两个字符串数组是否相等

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

通过执行交换操作检查两个字符串数组是否相等

给定两个大小相同的数组arr[]brr[]包含相等长度的字符串。在一次操作中,来自brr[]中任意字符串的任意两个字符可以相互交换,或者交换 brr[] 中任意两个字符串。任务是找出是否可以使brr[]等于arr[]

例子:

方法:给定的问题可以通过使用贪心方法来解决。只有当一个字符串中每个字符的频率等于另一个字符串时,才能通过交换使两个字符串相等。如果两个字符串都已排序,则所有字符都已排列,然后只需查看两个排序后的字符串是否相等即可得到答案。请按照以下步骤解决给定的问题。

  • arr[]brr[]中的每个字符串进行排序。
  • 排序arr[]brr[]
  • 使用变量i迭代arr[]并为每个i
    • 检查是否arr[i] == brr[i]
      • 如果为真,则继续比较剩余的字符串。
      • 如果为 false,则表示至少有一个字符串不在brr[]中。因此返回false。
  • 检查后,返回true作为最终答案。

下面是上述方法的实现:

C++14
// C++ program for above approach
#include 
using namespace std;
 
// Function to check whether brr[] can be made
// equal to arr[] by doing swapping operations
bool checkEqualArrays(vector arr,
                      vector brr,
                      int N)
{
 
    // size variable to store size of string
    int size = arr[0].size();
 
    // iterate till N to sort strings
    for (int i = 0; i < N; i++) {
        // sort each string in arr[]
        sort(arr[i].begin(), arr[i].end());
 
        // sort each string in brr[]
        sort(brr[i].begin(), brr[i].end());
    }
 
    // Sort both the vectors so that all
    // the comparable strings will be arranged
    sort(arr.begin(), arr.end());
    sort(brr.begin(), brr.end());
 
    // iterate till N
    // to compare string in arr[]
    // and brr[]
    for (int i = 0; i < N; i++) {
 
        // Compare each string
        if (arr[i] != brr[i]) {
 
            // Return false because
            // if atleast one
            // string is not equal
            // then brr[] cannot
            // be converted to arr[]
            return false;
        }
    }
 
    // All the strings
    // are compared so at last
    // return true.
    return true;
}
 
// Driver code
int main()
{
 
    int N = 2;
    vector arr = { "bcd", "aac" };
    vector brr = { "aca", "dbc" };
 
    // Store the answer in variable
    bool ans = checkEqualArrays(arr, brr, N);
 
    if (ans)
        cout << "true";
    else
        cout << "false";
 
    return 0;
}


Python3
# Python3 program for above approach
 
# Function to check whether brr[] can be made
# equal to arr[] by doing swapping operations
def checkEqualArrays(arr,brr, N) :
 
    # size variable to store size of string
    size = len(arr[0]);
 
    # iterate till N to sort strings
    for i in range(N) :
         
        # sort each string in arr[]
        temp1 = list(arr[i]);
        temp1.sort();
        arr[i] = "".join(temp1)
 
        # sort each string in brr[]
        temp2 = list(brr[i]);
        temp2.sort();
        brr[i] = "".join(temp2);
 
    # Sort both the vectors so that all
    # the comparable strings will be arranged
    arr.sort()
    brr.sort()
 
    # iterate till N
    # to compare string in arr[]
    # and brr[]
    for i in range(N) :
 
        # Compare each string
        if (arr[i] != brr[i]) :
 
            # Return false because
            # if atleast one
            # string is not equal
            # then brr[] cannot
            # be converted to arr[]
            return False;
 
    # All the strings
    # are compared so at last
    # return true.
    return True;
 
# Driver code
if __name__ == "__main__" :
 
    N = 2;
    arr = [ "bcd", "aac" ];
    brr = [ "aca", "dbc" ];
 
    # Store the answer in variable
    ans = checkEqualArrays(arr, brr, N);
 
    if (ans) :
        print("true");
    else :
        print("false");
 
    # This code is contributed by AnkThon


Javascript


输出
true

时间复杂度: O(2* (N * logN) + 2 * (N * logM) ),其中 N 是数组的大小,M 是每个字符串的大小。
辅助空间: O(1)