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
// 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)
{
// 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<string> arr = { "bcd", "aac" };
vector<string> brr = { "aca", "dbc" };
// Store the answer in variable
bool ans = checkEqualArrays(arr, brr, N);
if (ans)
cout << "true";
else
cout << "false";
return 0;
}
Java
import java.util.*;
class Main {
// Function to check whether brr[] can be made
// equal to arr[] by doing swapping operations
static boolean checkEqualArrays(String[] arr,
String[] brr,
int N) {
// size variable to store size of string
int size = arr[0].length();
// iterate till N to sort strings
for (int i = 0; i < N; i++) {
// sort each string in arr[]
char[] charArr = arr[i].toCharArray();
Arrays.sort(charArr);
arr[i] = new String(charArr);
// sort each string in brr[]
charArr = brr[i].toCharArray();
Arrays.sort(charArr);
brr[i] = new String(charArr);
}
// Sort both the arrays so that all
// the comparable strings will be arranged
Arrays.sort(arr);
Arrays.sort(brr);
// iterate till N
// to compare string in arr[]
// and brr[]
for (int i = 0; i < N;i++){
// Compare each string
if (!arr[i].equals(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;
}
public static void main(String[] args) {
int N = 2;
String[] arr = { "bcd", "aac" };
String[] brr = { "aca", "dbc" };
// Store the answer in variable
boolean ans = checkEqualArrays(arr, brr, N);
if (ans)
System.out.println("true");
else
System.out.println("false");
}
}
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
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
// Function to check whether brr[] can be made
// equal to arr[] by doing swapping operations
static bool CheckEqualArrays(List<string> arr,
List<string> brr,
int N)
{
// size variable to store size of string
int size = arr[0].Length;
// iterate till N to sort strings
for (int i = 0; i < N; i++)
{
// sort each string in arr[]
arr[i] = new string(arr[i].OrderBy(c => c).ToArray());
// sort each string in brr[]
brr[i] = new string(brr[i].OrderBy(c => c).ToArray());
}
// Sort both the lists so that all
// the comparable strings will be arranged
arr.Sort();
brr.Sort();
// 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
static void Main(string[] args)
{
int N = 2;
List<string> arr = new List<string> { "bcd", "aac" };
List<string> brr = new List<string> { "aca", "dbc" };
// Store the answer in variable
bool ans = CheckEqualArrays(arr, brr, N);
if (ans)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
<script>
// Javascript program for above approach
// Function to check whether brr[] can be made
// equal to arr[] by doing swapping operations
function checkEqualArrays(arr, brr, N)
{
// size variable to store size of string
let size = arr[0].length;
// iterate till N to sort strings
for (let i = 0; i < N; i++)
{
// sort each string in arr[]
arr[i] = arr[i].split("").sort().join("")
// sort each string in brr[]
brr[i] = brr[i].split("").sort().join("")
}
// 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 (let 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
let N = 2;
let arr = [ "bcd", "aac" ];
let brr = [ "aca", "dbc" ];
// Store the answer in variable
let ans = checkEqualArrays(arr, brr, N);
if (ans)
document.write("true");
else
document.write("false");
// This code is contributed by saurabh_jaiswal.
</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)
Similar Reads
Check if two binary strings can be made equal by swapping 1s occurring before 0s Given two binary strings str1 and str2 having same length, the task is to find if it is possible to make the two binary strings str1 and str2 equal by swapping all 1s occurring at indices less than that of 0s index in the binary string str1. Examples: Input: str1 = "0110", str2 = "0011" Output: Poss
15+ min read
Check if two strings are permutation of each other Write a function to check whether two given strings are Permutation of each other or not. A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, "abcd" and "dabc" are Permutation of each other. We strongly recommend that
13 min read
Check if two arrays are permutations of each other Given two unsorted arrays of the same size, write a function that returns true if two arrays are permutations of each other, otherwise false. Examples: Input: arr1[] = {2, 1, 3, 5, 4, 3, 2} arr2[] = {3, 2, 2, 4, 5, 3, 1}Output: YesInput: arr1[] = {2, 1, 3, 5,} arr2[] = {3, 2, 2, 4}Output: NoWe stron
15+ min read
Check if a string can be made equal to another string by swapping or replacement of characters Given two strings S1 and S2, the task is to check if string S1 can be made equal to string S2 by swapping any pair of characters replacing any character in the string S1. If it is possible to make the string S1 equal to S2, then print "Yes". Otherwise, print "No". Examples: Input: S1 = âabcâ, S2 = â
8 min read
Check if two strings can be made equal by swapping pairs of adjacent characters Given two strings A and B of length N and M respectively and an array arr[] consisting of K integers, the task is to check if the string B can be obtained from the string A by swapping any pair of adjacent characters of the string A any number of times such that the swapped indices are not present i
15+ min read