# Pairs of strings which on concatenating contains each character of “string”

Given an array of strings arr[]. The task is to find the count of unordered pairs of strings (arr[i], arr[j]), which on concatenation contains each character of the string “string” at least once.

Examples:

Input: arr[] = { “s”, “ng”, “stri”}
Output: 1
(arr, arr) is the only pair which on concatenation
will contain every character of the string “string”
i.e. arr + arr = “ngstri”

Input: arr[] = { “stri”, “ing”, “string” }
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Store the given strings as bit masks i.e. a string “srin” will be stored as 101110 as ‘t’ and ‘g’ are missing so their corresponding bit will be 0. Now, create an array of size 64 which is the maximum possible value of the bitmasks obtained (0 (000000) to 63 (111111)). Now, the problem is reduced to finding the count of pairs of these bitmasks that give 63 (111111 in binary) as their OR value.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define MAX 64 ` ` `  `// Function to return the bitmask for the string ` `int` `getBitmask(string s) ` `{ ` `    ``int` `temp = 0; ` `    ``for` `(``int` `j = 0; j < s.length(); j++) { ` `        ``if` `(s[j] == ``'s'``) { ` `            ``temp = temp | (1); ` `        ``} ` `        ``else` `if` `(s[j] == ``'t'``) { ` `            ``temp = temp | (2); ` `        ``} ` `        ``else` `if` `(s[j] == ``'r'``) { ` `            ``temp = temp | (4); ` `        ``} ` `        ``else` `if` `(s[j] == ``'i'``) { ` `            ``temp = temp | (8); ` `        ``} ` `        ``else` `if` `(s[j] == ``'n'``) { ` `            ``temp = temp | (16); ` `        ``} ` `        ``else` `if` `(s[j] == ``'g'``) { ` `            ``temp = temp | (32); ` `        ``} ` `    ``} ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to return the count of pairs ` `int` `countPairs(string arr[], ``int` `n) ` `{ ` ` `  `    ``// bitMask[i] will store the count of strings ` `    ``// from the array whose bitmask is i ` `    ``int` `bitMask[MAX] = { 0 }; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``bitMask[getBitmask(arr[i])]++; ` ` `  `    ``// To store the count of pairs ` `    ``int` `cnt = 0; ` `    ``for` `(``int` `i = 0; i < MAX; i++) { ` `        ``for` `(``int` `j = i; j < MAX; j++) { ` ` `  `            ``// MAX - 1 = 63 i.e. 111111 in binary ` `            ``if` `((i | j) == (MAX - 1)) { ` ` `  `                ``// arr[i] cannot make s pair with itself ` `                ``// i.e. (arr[i], arr[i]) ` `                ``if` `(i == j) ` `                    ``cnt += ((bitMask[i] * bitMask[i] - 1) / 2); ` `                ``else` `                    ``cnt += (bitMask[i] * bitMask[j]); ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string arr[] = { ``"strrr"``, ``"strring"``, ``"gstrin"` `}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << countPairs(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the ` `// above approach ` `class` `GFG ` `{ ` ` `  `static` `int` `MAX = ``64``; ` ` `  `// Function to return the bitmask for the string ` `static` `int` `getBitmask(``char``[] s) ` `{ ` `    ``int` `temp = ``0``; ` `    ``for` `(``int` `j = ``0``; j < s.length; j++)  ` `    ``{ ` `        ``switch` `(s[j])  ` `        ``{ ` `            ``case` `'s'``: ` `                ``temp = temp | (``1``); ` `                ``break``; ` `            ``case` `'t'``: ` `                ``temp = temp | (``2``); ` `                ``break``; ` `            ``case` `'r'``: ` `                ``temp = temp | (``4``); ` `                ``break``; ` `            ``case` `'i'``: ` `                ``temp = temp | (``8``); ` `                ``break``; ` `            ``case` `'n'``: ` `                ``temp = temp | (``16``); ` `                ``break``; ` `            ``case` `'g'``: ` `                ``temp = temp | (``32``); ` `                ``break``; ` `            ``default``: ` `                ``break``; ` `        ``} ` `    ``} ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to return the count of pairs ` `static` `int` `countPairs(String arr[], ``int` `n) ` `{ ` ` `  `    ``// bitMask[i] will store the count of strings ` `    ``// from the array whose bitmask is i ` `    ``int` `[]bitMask = ``new` `int``[MAX]; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``bitMask[getBitmask(arr[i].toCharArray())]++; ` ` `  `    ``// To store the count of pairs ` `    ``int` `cnt = ``0``; ` `    ``for` `(``int` `i = ``0``; i < MAX; i++)  ` `    ``{ ` `        ``for` `(``int` `j = i; j < MAX; j++) ` `        ``{ ` ` `  `            ``// MAX - 1 = 63 i.e. 111111 in binary ` `            ``if` `((i | j) == (MAX - ``1``))  ` `            ``{ ` ` `  `                ``// arr[i] cannot make s pair with itself ` `                ``// i.e. (arr[i], arr[i]) ` `                ``if` `(i == j) ` `                    ``cnt += ((bitMask[i] * bitMask[i] - ``1``) / ``2``); ` `                ``else` `                    ``cnt += (bitMask[i] * bitMask[j]); ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``String arr[] = { ``"strrr"``, ``"strring"``, ``"gstrin"` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println(countPairs(arr, n)); ` `} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 implementation of the approach ` `MAX` `=` `64` ` `  `# Function to return the bitmask ` `# for the string ` `def` `getBitmask(s): ` ` `  `    ``temp ``=` `0` `    ``for` `j ``in` `range``(``len``(s)): ` `        ``if` `(s[j] ``=``=` `'s'``): ` `            ``temp ``=` `temp | ``1` `        ``elif` `(s[j] ``=``=` `'t'``): ` `            ``temp ``=` `temp | ``2` `        ``elif` `(s[j] ``=``=` `'r'``): ` `            ``temp ``=` `temp | ``4` `        ``elif` `(s[j] ``=``=` `'i'``): ` `            ``temp ``=` `temp | ``8` `        ``elif` `(s[j] ``=``=` `'n'``): ` `            ``temp ``=` `temp | ``16` `        ``elif` `(s[j] ``=``=` `'g'``): ` `            ``temp ``=` `temp | ``32` ` `  `    ``return` `temp ` ` `  `# Function to return the count of pairs ` `def` `countPairs(arr, n): ` ` `  `    ``# bitMask[i] will store the count of strings ` `    ``# from the array whose bitmask is i ` `    ``bitMask ``=` `[``0` `for` `i ``in` `range``(``MAX``)] ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``bitMask[getBitmask(arr[i])] ``+``=` `1` ` `  `    ``# To store the count of pairs ` `    ``cnt ``=` `0` `    ``for` `i ``in` `range``(``MAX``): ` `        ``for` `j ``in` `range``(i, ``MAX``): ` ` `  `            ``# MAX - 1 = 63 i.e. 111111 in binary ` `            ``if` `((i | j) ``=``=` `(``MAX` `-` `1``)): ` ` `  `                ``# arr[i] cannot make s pair with itself ` `                ``# i.e. (arr[i], arr[i]) ` `                ``if` `(i ``=``=` `j): ` `                    ``cnt ``+``=` `((bitMask[i] ``*`  `                             ``bitMask[i] ``-` `1``) ``/``/` `2``) ` `                ``else``: ` `                    ``cnt ``+``=` `(bitMask[i] ``*` `bitMask[j]) ` `             `  `    ``return` `cnt ` ` `  `# Driver code ` `arr ``=` `[``"strrr"``, ``"strring"``, ``"gstrin"``] ` `n ``=` `len``(arr) ` `print``(countPairs(arr, n)) ` ` `  `# This code is contributed by mohit kumar `

## C#

 `// C# implementation of the ` `// above approach  ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG ` `{ ` ` `  `static` `int` `MAX = 64; ` ` `  `// Function to return the bitmask for the string ` `static` `int` `getBitmask(``char``[] s) ` `{ ` `    ``int` `temp = 0; ` `    ``for` `(``int` `j = 0; j < s.Length; j++)  ` `    ``{ ` `        ``switch` `(s[j])  ` `        ``{ ` `            ``case` `'s'``: ` `                ``temp = temp | (1); ` `                ``break``; ` `            ``case` `'t'``: ` `                ``temp = temp | (2); ` `                ``break``; ` `            ``case` `'r'``: ` `                ``temp = temp | (4); ` `                ``break``; ` `            ``case` `'i'``: ` `                ``temp = temp | (8); ` `                ``break``; ` `            ``case` `'n'``: ` `                ``temp = temp | (16); ` `                ``break``; ` `            ``case` `'g'``: ` `                ``temp = temp | (32); ` `                ``break``; ` `            ``default``: ` `                ``break``; ` `        ``} ` `    ``} ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to return the count of pairs ` `static` `int` `countPairs(String []arr, ``int` `n) ` `{ ` ` `  `    ``// bitMask[i] will store the count of strings ` `    ``// from the array whose bitmask is i ` `    ``int` `[]bitMask = ``new` `int``[MAX]; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``bitMask[getBitmask(arr[i].ToCharArray())]++; ` ` `  `    ``// To store the count of pairs ` `    ``int` `cnt = 0; ` `    ``for` `(``int` `i = 0; i < MAX; i++)  ` `    ``{ ` `        ``for` `(``int` `j = i; j < MAX; j++) ` `        ``{ ` ` `  `            ``// MAX - 1 = 63 i.e. 111111 in binary ` `            ``if` `((i | j) == (MAX - 1))  ` `            ``{ ` ` `  `                ``// arr[i] cannot make s pair with itself ` `                ``// i.e. (arr[i], arr[i]) ` `                ``if` `(i == j) ` `                    ``cnt += ((bitMask[i] * bitMask[i] - 1) / 2); ` `                ``else` `                    ``cnt += (bitMask[i] * bitMask[j]); ` `            ``} ` `        ``} ` `    ``} ` `    ``return` `cnt; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``String []arr = { ``"strrr"``, ``"strring"``, ``"gstrin"` `}; ` `    ``int` `n = arr.Length; ` `    ``Console.WriteLine(countPairs(arr, n)); ` `} ` `} ` ` `  `// This code has been contributed by 29AjayKumar `

## PHP

 ` `

Output:

```3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.