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