# Check if all strings of an array can be made same by interchanging characters

• Difficulty Level : Basic
• Last Updated : 06 Jun, 2021

Given an array arr[] of size N consisting of equal length strings, the task is to check if it is possible to make all strings of the array can be equal or not by swapping any character of one string with any character of the same string or another string.
Note: Perform the operation 0 or more times.

Examples:

Input : arr[] = {“fdd”, “fhh”}
Output: Yes
Explanation:
Swap(arr, arr) then arr[]={“fhd”, “fdh”}
Swap(arr, arr) then arr[]={“fhd”, “fhd”}. Therefore, it is possible to make all strings equal.

Input: arr[] = {“fde”, “fhg”}
Output: No

Approach: The problem can be solved by counting the frequency of each character of the given array and check if it is divisible by N or not. Follow the steps below to solve the problem:

1. Initialize an array, hash={0} to store the frequency of characters.
2. Traverse hash[] array and check if the frequency of all characters is divisible by N or not.
3. If the frequency of all characters is divisible by N, then print Yes.
4. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function to check if all strings``// are equal after swap operations``bool` `checkEqual(string arr[], ``int` `N)``{``    ``// Stores the frequency``    ``// of characters``    ``int` `hash = { 0 };` `    ``// Stores the length of string``    ``int` `M = arr.length();` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``// Traverse each string``        ``for` `(``int` `j = 0; j < M; j++) {``            ``hash[arr[i][j]]++;``        ``}``    ``}` `    ``// Check if frequency of character``    ``// is divisible by N``    ``for` `(``int` `i = 0; i < 256; i++) {``        ``if` `(hash[i] % N != 0) {``            ``return` `false``;``        ``}``    ``}` `    ``return` `true``;``}` `// Driver Code``int` `main()``{` `    ``string arr[] = { ``"fdd"``, ``"fhh"` `};``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``if` `(checkEqual(arr, N)) {``        ``cout << ``"Yes"``;``    ``}``    ``else` `{``        ``cout << ``"No"``;``    ``}` `    ``return` `0;``}`

## Java

 `// Java Program to implement``// the above approach``class` `GFG{` `// Function to check if all Strings``// are equal after swap operations``static` `boolean` `checkEqual(String arr[],``                          ``int` `N)``{``  ``// Stores the frequency``  ``// of characters``  ``int` `hash[] = ``new` `int``[``256``];` `  ``// Stores the length of String``  ``int` `M = arr[``0``].length();` `  ``// Traverse the array``  ``for` `(``int` `i = ``0``; i < N; i++)``  ``{``    ``// Traverse each String``    ``for` `(``int` `j = ``0``; j < M; j++)``    ``{``      ``hash[arr[i].charAt(j)]++;``    ``}``  ``}` `  ``// Check if frequency of character``  ``// is divisible by N``  ``for` `(``int` `i = ``0``; i < ``256``; i++)``  ``{``    ``if` `(hash[i] % N != ``0``)``    ``{``      ``return` `false``;``    ``}``  ``}` `  ``return` `true``;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``  ``String arr[] = {``"fdd"``, ``"fhh"``};``  ``int` `N = arr.length;` `  ``if` `(checkEqual(arr, N))``  ``{``    ``System.out.print(``"Yes"``);``  ``}``  ``else``  ``{``    ``System.out.print(``"No"``);``  ``}``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to check if all strings``# are equal after swap operations``def` `checkEqual(arr, N):` `    ``# Stores the frequency``    ``# of characters``    ``hash` `=` `[``0``] ``*` `256` `    ``# Stores the length of string``    ``M ``=` `len``(arr[``0``])` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``        ` `        ``# Traverse each string``        ``for` `j ``in` `range``(M):``            ``hash``[``ord``(arr[i][j])] ``+``=` `1` `    ``# Check if frequency of character``    ``# is divisible by N``    ``for` `i ``in` `range``(``256``):``        ``if``(``hash``[i] ``%` `N !``=` `0``):``            ``return` `False` `    ``return` `True` `# Driver Code``arr ``=` `[ ``"fdd"``, ``"fhh"` `]``N ``=` `len``(arr)` `# Function call``if``(checkEqual(arr, N)):``    ``print``(``"Yes"``)``else``:``    ``print``(``"No"``)` `# This code is contributed by Shivam Singh`

## C#

 `// C# program to implement``// the above approach``using` `System;` `class` `GFG{` `// Function to check if all Strings``// are equal after swap operations``static` `bool` `checkEqual(String []arr,``                       ``int` `N)``{``    ` `    ``// Stores the frequency``    ``// of characters``    ``int` `[]hash = ``new` `int``;``    ` `    ``// Stores the length of String``    ``int` `M = arr.Length;``    ` `    ``// Traverse the array``    ``for``(``int` `i = 0; i < N; i++)``    ``{``        ` `        ``// Traverse each String``        ``for``(``int` `j = 0; j < M; j++)``        ``{``            ``hash[arr[i][j]]++;``        ``}``    ``}``    ` `    ``// Check if frequency of character``    ``// is divisible by N``    ``for``(``int` `i = 0; i < 256; i++)``    ``{``        ``if` `(hash[i] % N != 0)``        ``{``            ``return` `false``;``        ``}``    ``}``    ``return` `true``;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``String []arr = { ``"fdd"``, ``"fhh"` `};``    ``int` `N = arr.Length;``    ` `    ``if` `(checkEqual(arr, N))``    ``{``        ``Console.Write(``"Yes"``);``    ``}``    ``else``    ``{``        ``Console.Write(``"No"``);``    ``}``}``}` `// This code is contributed by Amit Katiyar`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up