Skip to content
Related Articles

Related Articles

Improve Article

Check if two array of string are equal by performing swapping operations

  • Last Updated : 12 Oct, 2021

Given two arrays arr[] and brr[] of the same size containing strings of equal lengths. In one operation any two characters from any string in brr[] can be swapped with each other or swap any two strings in brr[]. The task is to find whether brr[] can be made equal to arr[] or not. 


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: arr[] = { “bcd”, “aac” }, brr[] = { “aca”, “dbc” }
Output: true
Explanation: The following swapping operations are performed in brr[] to make arr[] equal to brr[]. 
swap ( brr[0][1], brr[0][2] ) –> brr[0] changes to “aac”, which is equal to arr[1].
swap ( brr[1][0], brr[1][1] ) –> brr[1] changes to “bdc”.
swap (brr[1][1], brr[1][2]) –> brr[1] changes to “bcd”, which is equal to arr[0].
swapping ( brr[0], brr[1] ) which changes brr[] to { “bcd”, “aac” } which is equal to arr[]. 

Therefore, brr[] can be made equal to arr[] by doing above operations. 

Input: arr[] = { “ab”, “c” }, brr = { “ac”, “b” }
Output: false

Approach: The given problem can be solved by using the Greedy approach. Two strings can be made equal by swapping only if the frequency of each character in one string is equal to the other string. If both strings are sorted then all the characters are arranged and then just seeing whether the two sorted strings are equal or not will the answer. Follow the steps below to solve the given problem. 

  • Sort each string in arr[] as well as in brr[]
  • Sort arr[] and brr[]
  • Iterate in arr[] with variable i and for each i
    • Check whether arr[i] == brr[i].
      • if true, continue comparing the remaining strings. 
      • if false, it means there is at least one string that is not in brr[]. Therefore return false. 
  • After checking, return true as the final answer. 

Below is the implementation of the above approach:


// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether brr[] can be made
// equal to arr[] by doing swapping operations
bool checkEqualArrays(vector<string> arr,
                      vector<string> brr,
                      int N)
    // siz varible to store siz of string
    int siz = 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 becaues
            // 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<string> arr = { "bcd", "aac" };
    vector<string> brr = { "aca", "dbc" };
    // Store the answer in varible
    bool ans = checkEqualArrays(arr, brr, N);
    if (ans)
        cout << "true";
        cout << "false";
    return 0;

Time Complexity: O(2* (N * logN) + 2 * (N * logM) ), where N is the size of array and M is the size of each string. 
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :