Pairs of complete strings in two sets of strings
Two strings are said to be complete if on concatenation, they contain all the 26 English alphabets. For example, “abcdefghi” and “jklmnopqrstuvwxyz” are complete as they together have all characters from ‘a’ to ‘z’.
We are given two sets of sizes n and m respectively and we need to find the number of pairs that are complete on concatenating each string from set 1 to each string from set 2.
Input : set1[] = {"abcdefgh", "geeksforgeeks",
"lmnopqrst", "abc"}
set2[] = {"ijklmnopqrstuvwxyz",
"abcdefghijklmnopqrstuvwxyz",
"defghijklmnopqrstuvwxyz"}
Output : 7
The total complete pairs that are forming are:
"abcdefghijklmnopqrstuvwxyz"
"abcdefghabcdefghijklmnopqrstuvwxyz"
"abcdefghdefghijklmnopqrstuvwxyz"
"geeksforgeeksabcdefghijklmnopqrstuvwxyz"
"lmnopqrstabcdefghijklmnopqrstuvwxyz"
"abcabcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
Method 1 (Naive method)
A simple solution is to consider all pairs of strings, concatenate them and then check if the concatenated string has all the characters from ‘a’ to ‘z’ by using a frequency array.
C++
// C++ implementation for find pairs of complete // strings. #include <iostream> using namespace std; // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] int countCompletePairs(string set1[], string set2[], int n, int m) { int result = 0; // Consider all pairs of both strings for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Create a concatenation of current pair string concat = set1[i] + set2[j]; // Compute frequencies of all characters // in the concatenated string. int frequency[26] = { 0 }; for (int k = 0; k < concat.length(); k++) frequency[concat[k] - 'a']++; // If frequency of any character is not // greater than 0, then this pair is not // complete. int i; for (i = 0; i < 26; i++) if (frequency[i] < 1) break; if (i == 26) result++; } } return result; } // Driver code int main() { string set1[] = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; string set2[] = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = sizeof(set1) / sizeof(set1[0]); int m = sizeof(set2) / sizeof(set2[0]); cout << countCompletePairs(set1, set2, n, m); return 0; } |
Java
// Java implementation for find pairs of complete // strings. class GFG { // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] static int countCompletePairs(String set1[], String set2[], int n, int m) { int result = 0; // Consider all pairs of both strings for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Create a concatenation of current pair String concat = set1[i] + set2[j]; // Compute frequencies of all characters // in the concatenated String. int frequency[] = new int[26]; for (int k = 0; k < concat.length(); k++) { frequency[concat.charAt(k) - 'a']++; } // If frequency of any character is not // greater than 0, then this pair is not // complete. int k; for (k = 0; k < 26; k++) { if (frequency[k] < 1) { break; } } if (k == 26) { result++; } } } return result; } // Driver code static public void main(String[] args) { String set1[] = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; String set2[] = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = set1.length; int m = set2.length; System.out.println(countCompletePairs(set1, set2, n, m)); } } // This code is contributed by PrinciRaj19992 |
C#
// C# implementation for find pairs of complete // strings. using System; class GFG { // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] static int countCompletePairs(string[] set1, string[] set2, int n, int m) { int result = 0; // Consider all pairs of both strings for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // Create a concatenation of current pair string concat = set1[i] + set2[j]; // Compute frequencies of all characters // in the concatenated String. int[] frequency = new int[26]; for (int k = 0; k < concat.Length; k++) { frequency[concat[k] - 'a']++; } // If frequency of any character is not // greater than 0, then this pair is not // complete. int l; for (l = 0; l < 26; l++) { if (frequency[l] < 1) { break; } } if (l == 26) { result++; } } } return result; } // Driver code static public void Main() { string[] set1 = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; string[] set2 = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = set1.Length; int m = set2.Length; Console.Write(countCompletePairs(set1, set2, n, m)); } } // This article is contributed by Ita_c. |
Output:
7
Method 2 (Optimized method using Bit Manipulation)
In this method, we compress frequency array into an integer. We assign each bit of that integer with a character and we set it to 1 when the character is found. We perform this for all the strings in both the sets. Finally we just compare the two integers in the sets and if on combining all the bits are set, they form a complete string pair.
C++
// C++ program to find count of complete pairs #include <iostream> using namespace std; // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] int countCompletePairs(string set1[], string set2[], int n, int m) { int result = 0; // con_s1[i] is going to store an integer whose // set bits represent presence/absence of characters // in string set1[i]. // Similarly con_s2[i] is going to store an integer // whose set bits represent presence/absence of // characters in string set2[i] int con_s1[n], con_s2[m]; // Process all strings in set1[] for (int i = 0; i < n; i++) { // initializing all bits to 0 con_s1[i] = 0; for (int j = 0; j < set1[i].length(); j++) { // Setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s1[i] = con_s1[i] | (1 << (set1[i][j] - 'a')); } } // Process all strings in set2[] for (int i = 0; i < m; i++) { // initializing all bits to 0 con_s2[i] = 0; for (int j = 0; j < set2[i].length(); j++) { // setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s2[i] = con_s2[i] | (1 << (set2[i][j] - 'a')); } } // assigning a variable whose all 26 (0..25) // bits are set to 1 long long complete = (1 << 26) - 1; // Now consider every pair of integer in con_s1[] // and con_s2[] and check if the pair is complete. for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // if all bits are set, the strings are // complete! if ((con_s1[i] | con_s2[j]) == complete) result++; } } return result; } // Driver code int main() { string set1[] = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; string set2[] = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = sizeof(set1) / sizeof(set1[0]); int m = sizeof(set2) / sizeof(set2[0]); cout << countCompletePairs(set1, set2, n, m); return 0; } |
Java
// Java program to find count of complete pairs class GFG { // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] static int countCompletePairs(String set1[], String set2[], int n, int m) { int result = 0; // con_s1[i] is going to store an integer whose // set bits represent presence/absence of characters // in string set1[i]. // Similarly con_s2[i] is going to store an integer // whose set bits represent presence/absence of // characters in string set2[i] int[] con_s1 = new int[n]; int[] con_s2 = new int[m]; // Process all strings in set1[] for (int i = 0; i < n; i++) { // initializing all bits to 0 con_s1[i] = 0; for (int j = 0; j < set1[i].length(); j++) { // Setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s1[i] = con_s1[i] | (1 << (set1[i].charAt(j) - 'a')); } } // Process all strings in set2[] for (int i = 0; i < m; i++) { // initializing all bits to 0 con_s2[i] = 0; for (int j = 0; j < set2[i].length(); j++) { // setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s2[i] = con_s2[i] | (1 << (set2[i].charAt(j) - 'a')); } } // assigning a variable whose all 26 (0..25) // bits are set to 1 long complete = (1 << 26) - 1; // Now consider every pair of integer in con_s1[] // and con_s2[] and check if the pair is complete. for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // if all bits are set, the strings are // complete! if ((con_s1[i] | con_s2[j]) == complete) { result++; } } } return result; } // Driver code public static void main(String args[]) { String set1[] = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; String set2[] = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = set1.length; int m = set2.length; System.out.println(countCompletePairs(set1, set2, n, m)); } } // This code contributed by Rajput-Ji |
C#
// C# program to find count of complete pairs using System; class GFG { // Returns count of complete pairs from set[0..n-1] // and set2[0..m-1] static int countCompletePairs(String[] set1, String[] set2, int n, int m) { int result = 0; // con_s1[i] is going to store an integer whose // set bits represent presence/absence of characters // in string set1[i]. // Similarly con_s2[i] is going to store an integer // whose set bits represent presence/absence of // characters in string set2[i] int[] con_s1 = new int[n]; int[] con_s2 = new int[m]; // Process all strings in set1[] for (int i = 0; i < n; i++) { // initializing all bits to 0 con_s1[i] = 0; for (int j = 0; j < set1[i].Length; j++) { // Setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s1[i] = con_s1[i] | (1 << (set1[i][j] - 'a')); } } // Process all strings in set2[] for (int i = 0; i < m; i++) { // initializing all bits to 0 con_s2[i] = 0; for (int j = 0; j < set2[i].Length; j++) { // setting the ascii code of char s[i][j] // to 1 in the compressed integer. con_s2[i] = con_s2[i] | (1 << (set2[i][j] - 'a')); } } // assigning a variable whose all 26 (0..25) // bits are set to 1 long complete = (1 << 26) - 1; // Now consider every pair of integer in con_s1[] // and con_s2[] and check if the pair is complete. for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // if all bits are set, the strings are // complete! if ((con_s1[i] | con_s2[j]) == complete) { result++; } } } return result; } // Driver code public static void Main(String[] args) { String[] set1 = { "abcdefgh", "geeksforgeeks", "lmnopqrst", "abc" }; String[] set2 = { "ijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", "defghijklmnopqrstuvwxyz" }; int n = set1.Length; int m = set2.Length; Console.WriteLine(countCompletePairs(set1, set2, n, m)); } } // This code has been contributed by 29AjayKumar |
Output:
7
This article is contributed by Rishabh Jain. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Character pairs from two strings with even sum
- Pairs of strings which on concatenating contains each character of "string"
- Count of pairs of strings which differ in exactly one position
- Print all pairs of anagrams in a given array of strings
- Count pairs of strings that satisfy the given conditions
- Minimum number of pairs required to make two strings same
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Search in an array of strings where non-empty strings are sorted
- Meta Strings (Check if two strings can become same after a swap in one string)
- Number of common base strings for two strings
- Count of strings that become equal to one of the two strings after one removal
- Distinct strings such that they contains given strings as sub-sequences
- Sets of pairs in C++
- Add two bit strings



