Dada una array arr[] de tamaño N que consta de strings de igual longitud, la tarea es verificar si es posible hacer que todas las strings de la array sean iguales o no intercambiando cualquier carácter de una string con cualquier carácter de la misma string u otra string.
Nota: Realice la operación 0 o más veces.
Ejemplos:
Entrada: arr[] = { “fdd”, “fhh”}
Salida: Sí
Explicación:
Swap(arr[0][1], arr[1][1]) luego arr[]={ “fhd”, “fdh ” }
Swap(arr[1][1], arr[1][2]) luego arr[]={ “fhd”, “fhd” }. Por lo tanto, es posible hacer que todas las strings sean iguales.Entrada: arr[] = { “fde”, “fhg” }
Salida: No
Enfoque: el problema se puede resolver contando la frecuencia de cada carácter de la array dada y verificando si es divisible por N o no. Siga los pasos a continuación para resolver el problema:
- Inicialice una array, hash[256]={0} para almacenar la frecuencia de los caracteres.
- Atraviese la array hash[] y verifique si la frecuencia de todos los caracteres es divisible por N o no.
- Si la frecuencia de todos los caracteres es divisible por N , imprima Sí .
- De lo contrario , imprima No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> 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[256] = { 0 }; // 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][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[0]); 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[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][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
<script> // Javascript program to implement // the above approach // Function to check if all strings // are equal after swap operations function checkEqual(arr, N) { // Stores the frequency // of characters var hash = Array(256).fill(0); // Stores the length of string var M = arr[0].length; // Traverse the array for(var i = 0; i < N; i++) { // Traverse each string for(var j = 0; j < M; j++) { hash[arr[i][j]]++; } } // Check if frequency of character // is divisible by N for(var i = 0; i < 256; i++) { if (hash[i] % N != 0) { return false; } } return true; } // Driver Code var arr = ["fdd", "fhh"]; var N = arr.length; if (checkEqual(arr, N)) { document.write("Yes"); } else { document.write("No"); } // This code is contributed by importantly </script>
Yes
Complejidad de Tiempo: O(N)
Espacio Auxiliar: O(1), ya que no se ha tomado espacio extra.
Publicación traducida automáticamente
Artículo escrito por shawavisek35 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA