Check if given strings can be made same by swapping two characters of same or different strings | 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
function isEqualStrings ( arr , N ) { let M = arr [ 0 ] . length ; let cntFreq = new Array ( 256 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { cntFreq [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] [ j ] - ' ' ] += 1 ; } } for ( let i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; if ( isEqualStrings ( arr , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }