Number of pairs with Pandigital Concatenation | Javascript program to count PanDigital pairs ; Stores digits present in string v [ i ] atleast once . We use a set as we only need digits which exist only once ( irrespective of reputation ) ; Calculate the mask by considering all digits existing atleast once ; Increment the frequency of this mask ; Returns number of pairs of strings resulting in Pandigital Concatenations ; All possible strings lie between 1 and 1023 so we iterate over every possible mask ; if the concatenation results in mask of Pandigital Concatenation , calculate all pairs formed with Masks i and j ; Since every pair is considers twice , we get rid of half of these ; Find frequencies of all masks in given vector of strings ; Return all possiblg concatenations . ; Driver code
const pandigitalMask = ( ( 1 << 10 ) - 1 ) ; function computeMaskFrequencies ( v , freq ) { for ( let i = 0 ; i < v . length ; i ++ ) { let mask = 0 ; let digits = new Set ( ) ; for ( let j = 0 ; j < v [ i ] . length ; j ++ ) digits . add ( ( v [ i ] [ j ] ) . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; for ( let it of digits ) { let digit = it ; mask += ( 1 << digit ) ; } if ( freq . has ( mask ) ) { freq . set ( mask , freq . get ( mask ) + 1 ) } else { freq . set ( mask , 1 ) } } } function pandigitalConcatenations ( freq ) { let ans = 0 ; for ( let i = 1 ; i <= 1023 ; i ++ ) { for ( let j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . has ( i ) && freq . has ( j ) ) { if ( i == j ) ans += ( freq . get ( i ) * ( freq . get ( i ) - 1 ) ) ; else ans += ( freq . get ( i ) * freq . get ( j ) ) ; } } } return Math . floor ( ans / 2 ) ; } function countPandigitalPairs ( v ) { let freq = new Map ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } let v = [ " " , " " , " " , " " ] ; document . write ( countPandigitalPairs ( v ) + " " ) ;