Open in App
Not now

# Number of pairs with Pandigital Concatenation

• Difficulty Level : Expert
• Last Updated : 05 Aug, 2022

A pair of strings when concatenated is said to be a ‘Pandigital Concatenation’ if their concatenation consists of all digits from (0 – 9) in any order at least once.The task is, given N strings, compute the number of pairs resulting in a ‘Pandigital Concatenation’.

Examples:

Input  : num[] = {"123567", "098234", "14765", "19804"}
Output : 3
The pairs, 1st and 2nd giving
(123567098234),1st and 4rd giving(12356719804) and
2nd and 3rd giving (09823414765),
on concatenation result in Pandigital Concatenations.

Input : num[] =  {"56789", "098345", "1234"}
Output : 0
None of the pairs on concatenation result in Pandigital
Concatenations.

Method 1 (Brute Force): A possible brute-force solution is to form all possible concatenations by forming all pairs in O(n2 and using a frequency array for digits (0 – 9), we check if each digit exists at least once in each concatenation formed for every pair.

Implementation:

## C++

 // C++ program to find all // Pandigital concatenations // of two strings. #include using namespace std;   // Checks if a given // string is Pandigital bool isPanDigital(string s) {     bool digits[10] = {false};     for (int i = 0; i < s.length(); i++)         digits[s[i] - '0'] = true;       // digit i is not present     // thus not pandigital     for (int i = 0; i <= 9; i++)         if (digits[i] == false)             return false;       return true; }   // Returns number of pairs // of strings resulting in // Pandigital Concatenations int countPandigitalPairs(vector &v) {     // iterate over all     // pair of strings     int pairs = 0;     for (int i = 0; i < v.size(); i++)         for (int j = i + 1; j < v.size(); j++)             if (isPanDigital(v[i] + v[j]))                 pairs++;     return pairs; }   // Driver code int main() {     vector v = {"123567", "098234",                         "14765", "19804"};     cout << countPandigitalPairs(v) << endl;     return 0; }

## Java

 // Java program to find all // Pandigital concatenations // of two strings. import java.io.*; import java.util.*;   class GFG {     static ArrayList v =                   new ArrayList();                         // Checks if a given     // string is Pandigital     static int isPanDigital(String s)     {         int digits[] = new int[10];                   for (int i = 0; i < s.length(); i++)             digits[s.charAt(i) -                         (int)'0'] = 1;               // digit i is not present         // thus not pandigital         for (int i = 0; i <= 9; i++)             if (digits[i] == 0)                 return 0;               return 1;     }           // Returns number of pairs     // of strings resulting in     // Pandigital Concatenations     static int countPandigitalPairs()     {         // iterate over all         // pair of strings         int pairs = 0;         for (int i = 0; i < v.size(); i++)             for (int j = i + 1;                      j < v.size(); j++)                 if (isPanDigital(v.get(i) +                                  v.get(j)) == 1)                     pairs++;         return pairs;     }           // Driver code     public static void main(String args[])     {         v.add("123567");         v.add("098234");         v.add("14765");         v.add("19804");         System.out.print(countPandigitalPairs());     } }   // This code is contributed // by Manish Shaw(manishshaw1)

## Python3

 # Python3 program to find all # Pandigital concatenations # of two strings.   # Checks if a given # is Pandigital def isPanDigital(s) :       digits = [False] * 10;       for i in range(0, len(s)) :         digits[int(s[i]) -                int('0')] = True       # digit i is not present     # thus not pandigital     for i in range(0, 10) :         if (digits[i] == False) :             return False       return True   # Returns number of pairs # of strings resulting in # Pandigital Concatenations def countPandigitalPairs(v) :       # iterate over all     # pair of strings     pairs = 0     for i in range(0, len(v)) :           for j in range (i + 1,                         len(v)) :                       if (isPanDigital(v[i] +                              v[j])) :                 pairs = pairs + 1     return pairs   # Driver code v = ["123567", "098234",         "14765", "19804"]   print (countPandigitalPairs(v))   # This code is contributed by # Manish Shaw(manishshaw1)

## C#

 // C# program to find all Pandigital // concatenations of two strings. using System; using System.Collections.Generic;   class GFG {     // Checks if a given     // string is Pandigital     static int isPanDigital(string s)     {         int []digits = new int[10];         Array.Clear(digits, 0, 10);         for (int i = 0; i < s.Length; i++)             digits[s[i] - (int)'0'] = 1;               // digit i is not present         // thus not pandigital         for (int i = 0; i <= 9; i++)             if (digits[i] == 0)                 return 0;               return 1;     }           // Returns number of pairs     // of strings resulting in     // Pandigital Concatenations     static int countPandigitalPairs(ref List v)     {         // iterate over all         // pair of strings         int pairs = 0;         for (int i = 0; i < v.Count; i++)             for (int j = i + 1; j < v.Count; j++)                 if (isPanDigital(v[i] + v[j]) == 1)                     pairs++;         return pairs;     }           // Driver code     static void Main()     {         List v = new List{"123567", "098234",                                           "14765", "19804"};         Console.WriteLine(countPandigitalPairs(ref v));     } }   // This code is contributed // by Manish Shaw(manishshaw1)



## Javascript



Output

3

Method 2 (Efficient):

Now we look for something better than the brute-force discussed above. Careful analysis suggests that, for every digit 0 – 9 to be present we have a mask as 1111111111 (i.e. all numbers 0-9 exist in the array of numbers

Digits -  0  1  2  3  4  5  6  7  8  9
|  |  |  |  |  |  |  |  |  |
Mask   -  1  1  1  1  1  1  1  1  1  1

Here 1 denotes that the corresponding digits
exists at-least once thus for all such Pandigital
Concatenations, this relationship should hold.
So we can represent 11...11 as a valid mask for
pandigital concatenations.

So now the approach is to represent every string as a mask of 10 bits where the ith bit is set if the ith digit exists in the string.

E.g., "11405" can be represented as
Digits -           0  1  2  3  4  5  6  7  8  9
|  |  |  |  |  |  |  |  |  |
Mask for 11405 -   1  1  0  0  1  1  0  0  0  0

The approach though may look complete is still not efficient as we still have to iterate over all pairs and check if the OR of these two strings results in the mask of a valid Pandigital Concatenation.

If we analyze the possible masks of all possible strings we can understand that every single string would be only comprised of digits 0 â€“ 9, so every number can at max contain all digits 0 to 9 at least once thus the mask of such a number would be 1111111111 (1023 in decimal). Thus, in the decimal system all masks exit in (0 – 1023].

Now we just have to maintain a frequency array to store the number of times a mask exists in the array of strings.

Let two masks be i and j with frequencies freqi and freqj respectively,
If (i OR j) = Maskpandigital concatenation
Then,
Number of Pairs = freqi * freqj

Implementation:

## Javascript



Output

3

Complexity : O(N * |s| + 1023 * 1023) where |s| gives length of strings in the array.

My Personal Notes arrow_drop_up
Related Articles