Check if two array of string are equal by performing swapping operations
Last Updated :
03 Mar, 2023
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.
Example:
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++14
#include <bits/stdc++.h>
using namespace std;
bool checkEqualArrays(vector<string> arr,
vector<string> brr,
int N)
{
int size = arr[0].size();
for ( int i = 0; i < N; i++) {
sort(arr[i].begin(), arr[i].end());
sort(brr[i].begin(), brr[i].end());
}
sort(arr.begin(), arr.end());
sort(brr.begin(), brr.end());
for ( int i = 0; i < N; i++) {
if (arr[i] != brr[i]) {
return false ;
}
}
return true ;
}
int main()
{
int N = 2;
vector<string> arr = { "bcd" , "aac" };
vector<string> brr = { "aca" , "dbc" };
bool ans = checkEqualArrays(arr, brr, N);
if (ans)
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.util.*;
class Main {
static boolean checkEqualArrays(String[] arr,
String[] brr,
int N) {
int size = arr[ 0 ].length();
for ( int i = 0 ; i < N; i++) {
char [] charArr = arr[i].toCharArray();
Arrays.sort(charArr);
arr[i] = new String(charArr);
charArr = brr[i].toCharArray();
Arrays.sort(charArr);
brr[i] = new String(charArr);
}
Arrays.sort(arr);
Arrays.sort(brr);
for ( int i = 0 ; i < N;i++){
if (!arr[i].equals(brr[i])) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
int N = 2 ;
String[] arr = { "bcd" , "aac" };
String[] brr = { "aca" , "dbc" };
boolean ans = checkEqualArrays(arr, brr, N);
if (ans)
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
def checkEqualArrays(arr,brr, N) :
size = len (arr[ 0 ]);
for i in range (N) :
temp1 = list (arr[i]);
temp1.sort();
arr[i] = "".join(temp1)
temp2 = list (brr[i]);
temp2.sort();
brr[i] = "".join(temp2);
arr.sort()
brr.sort()
for i in range (N) :
if (arr[i] ! = brr[i]) :
return False ;
return True ;
if __name__ = = "__main__" :
N = 2 ;
arr = [ "bcd" , "aac" ];
brr = [ "aca" , "dbc" ];
ans = checkEqualArrays(arr, brr, N);
if (ans) :
print ( "true" );
else :
print ( "false" );
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static bool CheckEqualArrays(List< string > arr,
List< string > brr,
int N)
{
int size = arr[0].Length;
for ( int i = 0; i < N; i++)
{
arr[i] = new string (arr[i].OrderBy(c => c).ToArray());
brr[i] = new string (brr[i].OrderBy(c => c).ToArray());
}
arr.Sort();
brr.Sort();
for ( int i = 0; i < N; i++)
{
if (arr[i] != brr[i])
{
return false ;
}
}
return true ;
}
static void Main( string [] args)
{
int N = 2;
List< string > arr = new List< string > { "bcd" , "aac" };
List< string > brr = new List< string > { "aca" , "dbc" };
bool ans = CheckEqualArrays(arr, brr, N);
if (ans)
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
|
Javascript
<script>
function checkEqualArrays(arr, brr, N)
{
let size = arr[0].length;
for (let i = 0; i < N; i++)
{
arr[i] = arr[i].split( "" ).sort().join( "" )
brr[i] = brr[i].split( "" ).sort().join( "" )
}
arr.sort()
brr.sort()
for (let i = 0; i < N; i++) {
if (arr[i] != brr[i]) {
return false ;
}
}
return true ;
}
let N = 2;
let arr = [ "bcd" , "aac" ];
let brr = [ "aca" , "dbc" ];
let ans = checkEqualArrays(arr, brr, N);
if (ans)
document.write( "true" );
else
document.write( "false" );
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...