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:
C++
#include <iostream>
using namespace std;
int countCompletePairs(string set1[], string set2[],
int n, int m)
{
int result = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
string concat = set1[i] + set2[j];
int frequency[26] = { 0 };
for ( int k = 0; k < concat.length(); k++)
frequency[concat[k] - 'a' ]++;
int i;
for (i = 0; i < 26; i++)
if (frequency[i] < 1)
break ;
if (i == 26)
result++;
}
}
return result;
}
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
class GFG {
static int countCompletePairs(String set1[], String set2[],
int n, int m)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
String concat = set1[i] + set2[j];
int frequency[] = new int [ 26 ];
for ( int k = 0 ; k < concat.length(); k++) {
frequency[concat.charAt(k) - 'a' ]++;
}
int k;
for (k = 0 ; k < 26 ; k++) {
if (frequency[k] < 1 ) {
break ;
}
}
if (k == 26 ) {
result++;
}
}
}
return result;
}
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));
}
}
|
Python3
def countCompletePairs(set1,set2,n,m):
result = 0
for i in range (n):
for j in range (m):
concat = set1[i] + set2[j]
frequency = [ 0 for i in range ( 26 )]
for k in range ( len (concat)):
frequency[ ord (concat[k]) - ord ( 'a' )] + = 1
k = 0
while (k< 26 ):
if (frequency[k] < 1 ):
break
k + = 1
if (k = = 26 ):
result + = 1
return result
set1 = [ "abcdefgh" , "geeksforgeeks" , "lmnopqrst" , "abc" ]
set2 = [ "ijklmnopqrstuvwxyz" , "abcdefghijklmnopqrstuvwxyz" , "defghijklmnopqrstuvwxyz" ]
n = len (set1)
m = len (set2)
print (countCompletePairs(set1, set2, n, m))
|
C#
using System;
class GFG {
static int countCompletePairs( string [] set1, string [] set2,
int n, int m)
{
int result = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
string concat = set1[i] + set2[j];
int [] frequency = new int [26];
for ( int k = 0; k < concat.Length; k++) {
frequency[concat[k] - 'a' ]++;
}
int l;
for (l = 0; l < 26; l++) {
if (frequency[l] < 1) {
break ;
}
}
if (l == 26) {
result++;
}
}
}
return result;
}
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));
}
}
|
Javascript
<script>
function countCompletePairs(set1,set2,n,m)
{
let result = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
let concat = set1[i] + set2[j];
let frequency = new Array(26);
for (let i= 0;i<26;i++)
{
frequency[i]=0;
}
for (let k = 0; k < concat.length; k++) {
frequency[concat[k].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
let k;
for (k = 0; k < 26; k++) {
if (frequency[k] < 1) {
break ;
}
}
if (k == 26) {
result++;
}
}
}
return result;
}
let set1=[ "abcdefgh" , "geeksforgeeks" ,
"lmnopqrst" , "abc" ];
let set2=[ "ijklmnopqrstuvwxyz" ,
"abcdefghijklmnopqrstuvwxyz" ,
"defghijklmnopqrstuvwxyz" ]
let n = set1.length;
let m=set2.length;
document.write(countCompletePairs(set1, set2, n, m));
</script>
|
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:
C++14
#include <iostream>
using namespace std;
int countCompletePairs(string set1[], string set2[],
int n, int m)
{
int result = 0;
int con_s1[n], con_s2[m];
for ( int i = 0; i < n; i++) {
con_s1[i] = 0;
for ( int j = 0; j < set1[i].length(); j++) {
con_s1[i] = con_s1[i] | (1 << (set1[i][j] - 'a' ));
}
}
for ( int i = 0; i < m; i++) {
con_s2[i] = 0;
for ( int j = 0; j < set2[i].length(); j++) {
con_s2[i] = con_s2[i] | (1 << (set2[i][j] - 'a' ));
}
}
long long complete = (1 << 26) - 1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if ((con_s1[i] | con_s2[j]) == complete)
result++;
}
}
return result;
}
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
class GFG {
static int countCompletePairs(String set1[], String set2[],
int n, int m)
{
int result = 0 ;
int [] con_s1 = new int [n];
int [] con_s2 = new int [m];
for ( int i = 0 ; i < n; i++) {
con_s1[i] = 0 ;
for ( int j = 0 ; j < set1[i].length(); j++) {
con_s1[i] = con_s1[i] | ( 1 << (set1[i].charAt(j) - 'a' ));
}
}
for ( int i = 0 ; i < m; i++) {
con_s2[i] = 0 ;
for ( int j = 0 ; j < set2[i].length(); j++) {
con_s2[i] = con_s2[i] | ( 1 << (set2[i].charAt(j) - 'a' ));
}
}
long complete = ( 1 << 26 ) - 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if ((con_s1[i] | con_s2[j]) == complete) {
result++;
}
}
}
return result;
}
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));
}
}
|
C#
using System;
class GFG {
static int countCompletePairs(String[] set1, String[] set2,
int n, int m)
{
int result = 0;
int [] con_s1 = new int [n];
int [] con_s2 = new int [m];
for ( int i = 0; i < n; i++) {
con_s1[i] = 0;
for ( int j = 0; j < set1[i].Length; j++) {
con_s1[i] = con_s1[i] | (1 << (set1[i][j] - 'a' ));
}
}
for ( int i = 0; i < m; i++) {
con_s2[i] = 0;
for ( int j = 0; j < set2[i].Length; j++) {
con_s2[i] = con_s2[i] | (1 << (set2[i][j] - 'a' ));
}
}
long complete = (1 << 26) - 1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if ((con_s1[i] | con_s2[j]) == complete) {
result++;
}
}
}
return result;
}
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));
}
}
|
Python3
def countCompletePairs(set1, set2, n, m):
result = 0
con_s1, con_s2 = [ 0 ] * n, [ 0 ] * m
for i in range (n):
con_s1[i] = 0
for j in range ( len (set1[i])):
con_s1[i] = con_s1[i] | ( 1 << ( ord (set1[i][j]) - ord ( 'a' )))
for i in range (m):
con_s2[i] = 0
for j in range ( len (set2[i])):
con_s2[i] = con_s2[i] | ( 1 << ( ord (set2[i][j]) - ord ( 'a' )))
complete = ( 1 << 26 ) - 1
for i in range (n):
for j in range (m):
if ((con_s1[i] | con_s2[j]) = = complete):
result + = 1
return result
if __name__ = = '__main__' :
set1 = [ "abcdefgh" , "geeksforgeeks" ,
"lmnopqrst" , "abc" ]
set2 = [ "ijklmnopqrstuvwxyz" ,
"abcdefghijklmnopqrstuvwxyz" ,
"defghijklmnopqrstuvwxyz" ]
n = len (set1)
m = len (set2)
print (countCompletePairs(set1, set2, n, m))
|
Javascript
<script>
function countCompletePairs(set1,set2,n,m)
{
let result = 0;
let con_s1 = new Array(n);
let con_s2 = new Array(m);
for (let i = 0; i < n; i++) {
con_s1[i] = 0;
for (let j = 0; j < set1[i].length; j++) {
con_s1[i] = con_s1[i] |
(1 << (set1[i][j].charCodeAt(0) - 'a' .charCodeAt(0)));
}
}
for (let i = 0; i < m; i++) {
con_s2[i] = 0;
for (let j = 0; j < set2[i].length; j++) {
con_s2[i] = con_s2[i] |
(1 << (set2[i][j].charCodeAt(0) - 'a' .charCodeAt(0)));
}
}
let complete = (1 << 26) - 1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if ((con_s1[i] | con_s2[j]) == complete) {
result++;
}
}
}
return result;
}
let set1=[ "abcdefgh" , "geeksforgeeks" ,
"lmnopqrst" , "abc" ];
let set2=[ "ijklmnopqrstuvwxyz" ,
"abcdefghijklmnopqrstuvwxyz" ,
"defghijklmnopqrstuvwxyz" ]
let n = set1.length;
let m = set2.length;
document.write(countCompletePairs(set1, set2, n, m));
</script>
|
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. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.