Maximize product of lengths of strings having no common characters | Function to count the number of set bits in the integer n ; Stores the count of set bits in n ; Return the count ; Function to find the maximum product of pair of strings having no common characters ; Stores the integer equivalent of the strings ; Traverse the array of strings ; Traverse the current string ; Store the current bit position in bits [ i ] ; Store the required result ; Traverse the array , bits [ ] to get all unique pairs ( i , j ) ; Check whether the strings have no common characters ; Update the overall maximum product ; Print the maximum product ; Driver Code
function countSetBits ( n ) { let count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } function maximumProduct ( words ) { let bits = new Array ( words . length ) . fill ( 0 ) ; for ( let i = 0 ; i < words . length ; i ++ ) { for ( let j = 0 ; j < words [ i ] . length ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; } } let result = 0 ; for ( let i = 0 ; i < bits . length ; i ++ ) { for ( let j = i + 1 ; j < bits . length ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { let L = countSetBits ( bits [ i ] ) ; let R = countSetBits ( bits [ j ] ) ; result = Math . max ( L * R , result ) ; } } } document . write ( result ) ; } let arr = [ " " , " " , " " , " " , " " , " " ] ; maximumProduct ( arr ) ;