Check if given strings can be made same by swapping two characters of same or different strings
Last Updated :
31 Mar, 2023
Given an array of equal-length strings, arr[] of size N, the task is to check if all the strings can be made equal by repeatedly swapping any pair of characters of same or different strings from the given array. If found to be true, then print “YES”. Otherwise, print “NO”.
Examples:
Input: arr[] = { “acbdd”, “abcee” }
Output: YES
Explanation:
Swapping arr[0][1] and arr[0][2] modifies arr[] to { “abcdd”, “abcee” }
Swapping arr[0][3] and arr[1][4] modifies arr[] to { “abced”, “abced” }
Therefore, the required output is “YES”.
Input: arr[] = { “abb”, “acc”, “abc” }
Output: YES
Explanation:
Swapping arr[0][2] with arr[1][1] modifies arr[] to { “abc”, “abc”, “abc” }.
Therefore, the required output is “YES”.
Approach: The idea is to count the frequency of each distinct character of all the strings and check if the frequency of each distinct character is divisible by N or not. If found to be true, then print “YES”. Otherwise, print “NO”. Follow the steps below to solve the problem:
- Initialize an array, say cntFreq[], to store the frequency of each distinct character of all the strings.
- Traverse the array and for every string encountered, count the frequency of each distinct character of the string.
- Traverse the array cntFreq[] using variable i. For every ith character, check if cntFreq[i] is divisible by N or not. If found to be false, then print “NO”.
- Otherwise, print “YES”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isEqualStrings(string arr[], int N)
{
int M = arr[0].length();
int cntFreq[256] = { 0 };
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
cntFreq[arr[i][j] - 'a' ]++;
}
}
for ( int i = 0; i < 256; i++) {
if (cntFreq[i] % N != 0) {
return false ;
}
}
return true ;
}
int main()
{
string arr[] = { "aab" , "bbc" , "cca" };
int N = sizeof (arr) / sizeof (arr[0]);
if (isEqualStrings(arr, N)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
class GFG{
static boolean isEqualStrings(String[] arr, int N)
{
int M = arr[ 0 ].length();
int [] cntFreq = new int [ 256 ];
for ( int i = 0 ; i < N; i++)
{
cntFreq[i] = 0 ;
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
cntFreq[arr[i].charAt(j) - 'a' ] += 1 ;
}
}
for ( int i = 0 ; i < 256 ; i++)
{
if (cntFreq[i] % N != 0 )
{
return false ;
}
}
return true ;
}
public static void main(String[] args)
{
String[] arr = { "aab" , "bbc" , "cca" };
int N = arr.length;
if (isEqualStrings(arr, N))
{
System.out.println( "YES" );
}
else
{
System.out.println( "NO" );
}
}
}
|
Python3
def isEqualStrings(arr, N):
M = len (arr[ 0 ])
cntFreq = [ 0 ] * 256
for i in range (N):
for j in range (M):
cntFreq[ ord (arr[i][j]) - ord ( 'a' )] + = 1
for i in range ( 256 ):
if (cntFreq[i] % N ! = 0 ):
return False
return True
if __name__ = = "__main__" :
arr = [ "aab" , "bbc" , "cca" ]
N = len (arr)
if isEqualStrings(arr, N):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG{
static bool isEqualStrings( string [] arr, int N)
{
int M = arr[0].Length;
int [] cntFreq = new int [256];
for ( int i = 0; i < N; i++)
{
cntFreq[i] = 0;
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
cntFreq[arr[i][j] - 'a' ] += 1;
}
}
for ( int i = 0; i < 256; i++)
{
if (cntFreq[i] % N != 0)
{
return false ;
}
}
return true ;
}
public static void Main()
{
string [] arr = { "aab" , "bbc" , "cca" };
int N = arr.Length;
if (isEqualStrings(arr, N))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function isEqualStrings(arr, N)
{
let M = arr[0].length;
let cntFreq = new Array(256).fill(0);
for (let i = 0; i < N; i++)
{
cntFreq[i] = 0;
}
for (let i = 0; i < N; i++)
{
for (let j = 0; j < M; j++)
{
cntFreq[arr[i][j] - 'a' ] += 1;
}
}
for (let i = 0; i < 256; i++)
{
if (cntFreq[i] % N != 0)
{
return false ;
}
}
return true ;
}
let arr = [ "aab" , "bbc" , "cca" ];
let N = arr.length;
if (isEqualStrings(arr, N))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(N * M + 256), where M is the length of the string
Auxiliary Space: O(256)
Approach 2: Dynamic Programming:
The given problem can be solved using a dynamic programming (DP) approach as well. The idea is to first check if all the strings have the same length or not. If not, then we cannot make them equal by swapping any pair of characters. Otherwise, we can proceed as follows:
- For each string S in the array, create a frequency count array freq[S] of size 26 (one for each lowercase English alphabet). freq[S][i] stores the frequency of the i-th lowercase English alphabet in the string S.
- For each pair of distinct strings (S1, S2) in the array, check if they have the same frequency count arrays or not. If not, then we cannot make all the strings equal by swapping any pair of characters.
- If all pairs of distinct strings in the array have the same frequency count arrays, then we can make all the strings equal by swapping any pair of characters. This is because we can swap any two characters that are not equal in any pair of distinct strings (S1, S2) to obtain a new pair of strings (S1′, S2′) such that S1′ and S2′ have the same frequency count arrays as S1 and S2, respectively. Then we can repeat this process for all other pairs of distinct strings until all strings have the same frequency count array.
C++
#include <bits/stdc++.h>
using namespace std;
bool isEqualStrings(string arr[], int N) {
int M = arr[0].length();
int cntFreq[26] = { 0 };
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
cntFreq[arr[i][j] - 'a' ]++;
}
}
for ( int i = 0; i < 26; i++) {
if (cntFreq[i] % N != 0) {
return false ;
}
}
return true ;
}
int main() {
string arr[] = { "aab" , "bbc" , "cca" };
int N = sizeof (arr) / sizeof (arr[0]);
if (isEqualStrings(arr, N)) {
cout << "YES" ;
} else {
cout << "NO" ;
}
return 0;
}
|
Java
public class Main {
public static boolean isEqualStrings(String[] arr, int N) {
int M = arr[ 0 ].length();
int [] cntFreq = new int [ 26 ];
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
cntFreq[arr[i].charAt(j) - 'a' ]++;
}
}
for ( int i = 0 ; i < 26 ; i++) {
if (cntFreq[i] % N != 0 ) {
return false ;
}
}
return true ;
}
public static void main(String[] args) {
String[] arr = { "aab" , "bbc" , "cca" };
int N = arr.length;
if (isEqualStrings(arr, N)) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
}
}
|
Python3
def isEqualStrings(arr, N):
M = len (arr[ 0 ])
cntFreq = [ 0 ] * 26
for i in range (N):
for j in range (M):
cntFreq[ ord (arr[i][j]) - ord ( 'a' )] + = 1
for i in range ( 26 ):
if cntFreq[i] % N ! = 0 :
return False
return True
arr = [ "aab" , "bbc" , "cca" ]
N = len (arr)
if isEqualStrings(arr, N):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class MainClass {
static bool IsEqualStrings( string [] arr, int N) {
int M = arr[0].Length;
int [] cntFreq = new int [26];
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
cntFreq[arr[i][j] - 'a' ]++;
}
}
for ( int i = 0; i < 26; i++) {
if (cntFreq[i] % N != 0) {
return false ;
}
}
return true ;
}
public static void Main( string [] args) {
string [] arr = { "aab" , "bbc" , "cca" };
int N = arr.Length;
if (IsEqualStrings(arr, N)) {
Console.WriteLine( "YES" );
} else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
function isEqualStrings(arr, N) {
let M = arr[0].length;
let cntFreq = new Array(26).fill(0);
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
cntFreq[arr[i][j].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
}
for (let i = 0; i < 26; i++) {
if (cntFreq[i] % N !== 0) {
return false ;
}
}
return true ;
}
let arr = [ "aab" , "bbc" , "cca" ];
let N = arr.length;
if (isEqualStrings(arr, N)) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Time Complexity: O(N * M), where M is the length of the string
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...