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
using System ; using System . Collections . Generic ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( List < String > v , Dictionary < int , int > freq ) { for ( int i = 0 ; i < v . Count ; i ++ ) { int mask = 0 ; HashSet < int > digits = new HashSet < int > ( ) ; for ( int j = 0 ; j < v [ i ] . Length ; j ++ ) digits . Add ( v [ i ] [ j ] - '0' ) ; foreach ( int it in digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . ContainsKey ( mask ) ) { freq [ mask ] ++ ; } else { freq . Add ( mask , 1 ) ; } } } static int pandigitalConcatenations ( Dictionary < int , int > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . ContainsKey ( j ) && freq . ContainsKey ( i ) ) { if ( i == j ) ans += ( freq [ i ] * ( freq [ i ] - 1 ) ) ; else ans += ( freq [ i ] * freq [ j ] ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( List < String > v ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void Main ( String [ ] args ) { List < String > v = new List < String > ( ) ; v . Add ( "123567" ) ; v . Add ( "098234" ) ; v . Add ( "14765" ) ; v . Add ( "19804" ) ; Console . Write ( countPandigitalPairs ( v ) + " STRNEWLINE " ) ; } }