Check if Strings can be made equal by interchanging Characters
Last Updated :
19 Jul, 2022
Given an array of N strings, the task is to find if it is possible to make all the strings equal by performing the following operations:
- Remove a character from ith string and insert it in an arbitrary position in jth string. (i and j can be the same)
- You may perform this operation any number of times.
Examples:
Input: N = 2, S[] = {“caa”, “cbb”}
Output: YES
Explanation: It is possible to exchange the ‘a’ of first string with ‘b’ in the second string to make both strings equal.
The strings can be made “cab”and “cab” or “cba” and “cba”.
Input: N = 3, S[] = {“cba”, “cba”, “cbb”}
Output: NO
Explanation: It is impossible to make all strings equal.
Approach: The idea to solve the problem is as follows:
Due to the operations, any string can be arranged in any way. So to make the strings equal all the strings should have all the characters equal number of times.
So they can be made equal if the frequency of each character in all the strings must be a multiple of N.
Follow the steps mentioned below to implement the problem:
- Traverse through the array of strings and for each string do the following:
- Traverse through that string.
- Count the frequency of each character.
- If the frequencies of all the characters of all the strings are multiple of N then only they can be made equal.
- Otherwise, return that it is impossible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool sameString( int N, string s[])
{
vector< int > cnt(26);
for ( int i = 0; i < N; ++i) {
for ( char ch : s[i]) {
++cnt[ch - 'a' ];
}
}
bool ans = true ;
for ( int i = 0; i < 26; ++i) {
if (cnt[i] % N != 0) {
ans = false ;
break ;
}
}
return ans;
}
int main()
{
int N = 3;
string S[] = { "cba" , "cba" , "cbb" };
bool ans;
ans = sameString(N, S);
cout << (ans ? "YES" : "NO" );
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean sameString( int N, String s[])
{
int [] cnt = new int [ 26 ];
for ( int i = 0 ; i < N; ++i) {
String st = s[i];
for ( int j = 0 ;j<st.length();j++) {
++cnt[st.charAt(j) - 'a' ];
}
}
boolean ans = true ;
for ( int i = 0 ; i < 26 ; ++i) {
if (cnt[i] % N != 0 ) {
ans = false ;
break ;
}
}
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
String S[] = { "cba" , "cba" , "cbb" };
boolean ans;
ans = sameString(N, S);
System.out.print((ans ? "YES" : "NO" ));
}
}
|
Python3
def sameString(N, s) :
cnt = [ 0 ] * 26 ;
for i in range (N):
for ch in s[i] :
cnt[ ord (ch) - ord ( 'a' )] + = 1 ;
ans = True ;
for i in range ( 26 ) :
if (cnt[i] % N ! = 0 ) :
ans = False ;
break ;
return ans;
if __name__ = = "__main__" :
N = 3 ;
S = [ "cba" , "cba" , "cbb" ];
ans = sameString(N, S);
if ans:
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static bool sameString( int N, string [] s)
{
int [] cnt = new int [26];
for ( int i = 0; i < N; i++) {
String st = s[i];
for ( int j = 0; j < st.Length; j++) {
++cnt[st[j] - 'a' ];
}
}
bool ans = true ;
for ( int i = 0; i < 26; i++) {
if (cnt[i] % N != 0) {
ans = false ;
break ;
}
}
return ans;
}
static public void Main()
{
int N = 3;
string [] S = new string [3] { "cba" , "cba" , "cbb" };
bool ans;
ans = sameString(N, S);
Console.WriteLine((ans ? "YES" : "NO" ));
}
}
|
Javascript
<script>
const sameString = (N, s) => {
let cnt = new Array(26).fill(0);
for (let i = 0; i < N; ++i) {
for (let ch in s[i]) {
++cnt[s[i].charCodeAt(ch) - 'a' .charCodeAt(0)];
}
}
let ans = true ;
for (let i = 0; i < 26; ++i) {
if (cnt[i] % N != 0) {
ans = false ;
break ;
}
}
return ans;
}
let N = 3;
let S = [ "cba" , "cba" , "cbb" ];
let ans;
ans = sameString(N, S);
if (ans) document.write( "YES" );
else document.write( "NO" );
</script>
|
Time Complexity: O(N * M) Where M is the maximum length of a string
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...