Number of pairs with Pandigital Concatenation | Java 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
import java . util . * ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( Vector < String > v , HashMap < Integer , Integer > freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; HashSet < Integer > digits = new HashSet < > ( ) ; for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) digits . add ( v . get ( i ) . charAt ( j ) - '0' ) ; for ( int it : digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . containsKey ( mask ) ) { freq . put ( mask , freq . get ( mask ) + 1 ) ; } else { freq . put ( mask , 1 ) ; } } } static int pandigitalConcatenations ( HashMap < Integer , Integer > 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 . get ( i ) * ( freq . get ( i ) - 1 ) ) ; else ans += ( freq . get ( i ) * freq . get ( j ) ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( Vector < String > v ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( v ) + "NEW_LINE"); } }