Number of pairs with Pandigital Concatenation | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; void computeMaskFrequencies ( vector < string > v , map < int , int > & freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; unordered_set < int > digits ; for ( int j = 0 ; j < v [ i ] . size ( ) ; j ++ ) digits . insert ( v [ i ] [ j ] - '0' ) ; for ( auto it = digits . begin ( ) ; it != digits . end ( ) ; it ++ ) { int digit = ( * it ) ; mask += ( 1 << digit ) ; } freq [ mask ] ++ ; } } int pandigitalConcatenations ( map < int , int > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask ) { if ( i == j ) ans += ( freq [ i ] * ( freq [ i ] - 1 ) ) ; else ans += ( freq [ i ] * freq [ j ] ) ; } } } return ans / 2 ; } int countPandigitalPairs ( vector < string > v ) { map < int , int > freq ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } int main ( ) { vector < string > v = { "123567" , "098234" , "14765" , "19804" } ; cout << countPandigitalPairs ( v ) << endl ; return 0 ; }