VOOZH about

URL: https://www.geeksforgeeks.org/dsa/pairs-of-complete-strings-in-two-sets-of-strings/

⇱ Pairs of complete strings in two sets of strings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Pairs of complete strings in two sets of strings

Last Updated : 14 Apr, 2023

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.  

Implementation:


Output
7

Time Complexity: O(n * m * k)
Auxiliary Space: O(1)

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.

Implementation:


Output
7

Time Complexity: O(n*m), where n is the size of the first set and m is the size of the second set.
Auxiliary Space: O(n)

This article is contributed by Rishabh Jain.  

Comment