Check if given strings can be made same by swapping two characters of same or different strings | Java program to implement the above approach ; Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] . charAt ( j ) ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call
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 " ) ; } } }