Count pair of strings whose concatenation has every vowel
Given an array arr[] of N strings. The task is to find the count of all possible pairs of strings such that their concatenations has every vowel at least once.
Examples:
Input: str[] = {“oakitie”, “aemiounau”, “aazuaxvbga”, “eltdgo”}
Output: 4
Explanation:
Concatenation of pair of string are:
1. (“oakitie”, “aazuaxvbga”),
2. (“oakitie”, “aemiounau”),
3. (“aazuaxvbga”, “aemiounau”),
4. (“aemiounau”, “eltdgo”)
They all have the vowel at least once.
Input: str[] = {“aaweiolkju”, “oxdfgujkmi”}
Output: 1
Explanation:
Concatenation of pair of string (“aaweiolkju”, “oxdfgujkmi”) has all the vowel character at least once.
Naive Approach: The idea is to generate all possible pairs from the given array and check if the concatenation of every possible pair of two strings has all the vowels at least once or not. If yes then include this in the count. Print the count after all the operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int good_pair(string str[], int N)
{
int countStr = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
string res = str[i] + str[j];
int vowel[5] = { 0 };
for ( int k = 0;
k < res.length(); k++) {
if (res[k] == 'a' )
vowel[0] = 1;
else if (res[k] == 'e' )
vowel[1] = 1;
else if (res[k] == 'i' )
vowel[2] = 1;
else if (res[k] == 'o' )
vowel[3] = 1;
else if (res[k] == 'u' )
vowel[4] = 1;
}
int temp = 0;
for ( int ind = 0; ind < 5; ind++) {
if (vowel[ind] == 1)
temp++;
}
if (temp == 5)
countStr++;
}
}
return countStr;
}
int main()
{
string arr[] = { "aaweiolkju" , "oxdfgujkmi" };
int N = sizeof (arr) / sizeof (arr[0]);
cout << good_pair(arr, N);
}
|
Java
import java.io.*;
class GFG{
static int good_pair(String str[], int N)
{
int countStr = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
String res = str[i] + str[j];
int vowel[] = new int [ 5 ];
for ( int k = 0 ;
k < res.length(); k++)
{
if (res.charAt(k) == 'a' )
vowel[ 0 ] = 1 ;
else if (res.charAt(k) == 'e' )
vowel[ 1 ] = 1 ;
else if (res.charAt(k) == 'i' )
vowel[ 2 ] = 1 ;
else if (res.charAt(k) == 'o' )
vowel[ 3 ] = 1 ;
else if (res.charAt(k) == 'u' )
vowel[ 4 ] = 1 ;
}
int temp = 0 ;
for ( int ind = 0 ; ind < 5 ; ind++)
{
if (vowel[ind] == 1 )
temp++;
}
if (temp == 5 )
countStr++;
}
}
return countStr;
}
public static void main(String[] args)
{
String arr[] = { "aaweiolkju" , "oxdfgujkmi" };
int N = arr.length;
System.out.print(good_pair(arr, N));
}
}
|
Python3
def good_pair(st, N):
countStr = 0
for i in range (N):
for j in range (i + 1 , N):
res = st[i] + st[j]
vowel = [ 0 ] * 5
for k in range ( len (res)):
if (res[k] = = 'a' ):
vowel[ 0 ] = 1
elif (res[k] = = 'e' ):
vowel[ 1 ] = 1
elif (res[k] = = 'i' ):
vowel[ 2 ] = 1
elif (res[k] = = 'o' ):
vowel[ 3 ] = 1
elif (res[k] = = 'u' ):
vowel[ 4 ] = 1
temp = 0
for ind in range ( 5 ):
if (vowel[ind] = = 1 ):
temp + = 1
if (temp = = 5 ):
countStr + = 1
return countStr
if __name__ = = "__main__" :
arr = [ "aaweiolkju" , "oxdfgujkmi" ]
N = len (arr)
print (good_pair(arr, N))
|
C#
using System;
class GFG{
static int good_pair(String []str, int N)
{
int countStr = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
String res = str[i] + str[j];
int []vowel = new int [5];
for ( int k = 0;
k < res.Length; k++)
{
if (res[k] == 'a' )
vowel[0] = 1;
else if (res[k] == 'e' )
vowel[1] = 1;
else if (res[k] == 'i' )
vowel[2] = 1;
else if (res[k] == 'o' )
vowel[3] = 1;
else if (res[k] == 'u' )
vowel[4] = 1;
}
int temp = 0;
for ( int ind = 0; ind < 5; ind++)
{
if (vowel[ind] == 1)
temp++;
}
if (temp == 5)
countStr++;
}
}
return countStr;
}
public static void Main(String[] args)
{
String []arr = { "aaweiolkju" , "oxdfgujkmi" };
int N = arr.Length;
Console.Write(good_pair(arr, N));
}
}
|
Javascript
<script>
function good_pair(str, N)
{
let countStr = 0;
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
let res = str[i] + str[j];
let vowel = new Array(5);
vowel.fill(0);
for (let k = 0; k < res.length; k++)
{
if (res[k] == 'a' )
vowel[0] = 1;
else if (res[k] == 'e' )
vowel[1] = 1;
else if (res[k] == 'i' )
vowel[2] = 1;
else if (res[k] == 'o' )
vowel[3] = 1;
else if (res[k] == 'u' )
vowel[4] = 1;
}
let temp = 0;
for (let ind = 0; ind < 5; ind++)
{
if (vowel[ind] == 1)
temp++;
}
if (temp == 5)
countStr++;
}
}
return countStr;
}
let arr = [ "aaweiolkju" , "oxdfgujkmi" ];
let N = arr.length;
document.write(good_pair(arr, N));
</script>
|
Time Complexity: O(N4)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above method the idea is to use Hashing. Below are the steps:
- Create a hash hash[] of length 32 with all elements as 0.
- Use hashing by taking a variable weight and performing Bitwise OR operation on this variable with powers of 2 while traversing each string as:
for 'a': Weight | 1,
for 'e': Weight | 2,
for 'i': Weight | 4,
for 'o': Weight | 8,
for 'u': Weight | 16
- The value of weight is a hash value which will indicate the combination of vowels the string contains.
For Example:
"aeiouau": Weight = 31, as it has all the vowels and
"oaiie": Weight = 15, as it has all the vowels except 'u'
- Increase index denoted by weight in the hash[] by 1 and the elements are the count of the string with indices as their hash value.
- So, all pairs of the hash value that is indices (i, j) which give Bitwise OR as 31 are the pairs that have all the vowels at least once in them. Let the count of such string be countStr. So, the count is given by:
countStr = hash[i] * hash[j]
- Add the count of each pairs using the formula in the above step.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int good_pairs(string str[], int N)
{
int arr[32] = { 0 }, strCount = 0;
for ( int i = 0; i < N; i++) {
int Weight = 0;
for ( int j = 0;
j < str[i].size(); j++) {
switch (str[i][j]) {
case 'a' :
Weight = Weight | 1;
break ;
case 'e' :
Weight = Weight | 2;
break ;
case 'i' :
Weight = Weight | 4;
break ;
case 'o' :
Weight = Weight | 8;
break ;
case 'u' :
Weight = Weight | 16;
break ;
}
}
arr[Weight]++;
}
for ( int i = 0; i < 32; i++) {
for ( int j = i + 1; j < 32; j++) {
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
strCount += (arr[31] * (arr[31] - 1)) / 2;
return strCount;
}
int main()
{
string str[] = { "aaweiolkju" , "oxdfgujkmi" };
int N = sizeof (str) / sizeof (str[0]);
cout << good_pairs(str, N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
public static int good_pairs(String[] str,
int N)
{
int arr[] = new int [ 32 ];
int strCount = 0 ;
for ( int i = 0 ; i < N; i++)
{
int Weight = 0 ;
for ( int j = 0 ;
j < str[i].length(); j++)
{
switch (str[i].charAt(j))
{
case 'a' :
Weight = Weight | 1 ;
break ;
case 'e' :
Weight = Weight | 2 ;
break ;
case 'i' :
Weight = Weight | 4 ;
break ;
case 'o' :
Weight = Weight | 8 ;
break ;
case 'u' :
Weight = Weight | 16 ;
break ;
}
}
arr[Weight]++;
}
for ( int i = 0 ; i < 32 ; i++)
{
for ( int j = i + 1 ; j < 32 ; j++)
{
if ((i | j) == 31 )
strCount += arr[i] * arr[j];
}
}
strCount += (arr[ 31 ] * (arr[ 31 ] - 1 )) / 2 ;
return strCount;
}
public static void main(String[] args)
{
String str[] = { "aaweiolkju" , "oxdfgujkmi" };
int N = str.length;
System.out.println(good_pairs(str, N));
}
}
|
Python3
def good_pairs( Str , N):
arr = [ 0 for i in range ( 32 )]
strCount = 0
for i in range (N):
Weight = 0
for j in range ( len ( Str [i])):
switcher = {
'a' : 1 ,
'e' : 2 ,
'i' : 4 ,
'o' : 8 ,
'u' : 16 ,
}
Weight = Weight | switcher.get( Str [i][j], 0 )
arr[Weight] + = 1
for i in range ( 32 ):
for j in range (i + 1 , 32 ):
if ((i | j) = = 31 ):
strCount + = arr[i] * arr[j]
strCount + = int ((arr[ 31 ] * (arr[ 31 ] - 1 )) / 2 )
return strCount
Str = [ "aaweiolkju" , "oxdfgujkmi" ]
N = len ( Str )
print (good_pairs( Str , N))
|
C#
using System;
class GFG{
public static int good_pairs( string [] str,
int N)
{
int [] arr = new int [32];
int strCount = 0;
for ( int i = 0; i < N; i++)
{
int Weight = 0;
for ( int j = 0;
j < str[i].Length; j++)
{
switch (str[i][j])
{
case 'a' :
Weight = Weight | 1;
break ;
case 'e' :
Weight = Weight | 2;
break ;
case 'i' :
Weight = Weight | 4;
break ;
case 'o' :
Weight = Weight | 8;
break ;
case 'u' :
Weight = Weight | 16;
break ;
}
}
arr[Weight]++;
}
for ( int i = 0; i < 32; i++)
{
for ( int j = i + 1; j < 32; j++)
{
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
strCount += (arr[31] * (arr[31] - 1)) / 2;
return strCount;
}
public static void Main( string [] args)
{
string [] str = { "aaweiolkju" , "oxdfgujkmi" };
int N = str.Length;
Console.Write(good_pairs(str, N));
}
}
|
Javascript
<script>
function good_pairs(str, N)
{
let arr = new Array(32);
arr.fill(0);
let strCount = 0;
for (let i = 0; i < N; i++)
{
let Weight = 0;
for (let j = 0; j < str[i].length; j++)
{
switch (str[i][j])
{
case 'a' :
Weight = Weight | 1;
break ;
case 'e' :
Weight = Weight | 2;
break ;
case 'i' :
Weight = Weight | 4;
break ;
case 'o' :
Weight = Weight | 8;
break ;
case 'u' :
Weight = Weight | 16;
break ;
}
}
arr[Weight]++;
}
for (let i = 0; i < 32; i++)
{
for (let j = i + 1; j < 32; j++)
{
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
strCount += parseInt((arr[31] * (arr[31] - 1)) / 2, 10);
return strCount;
}
let str = [ "aaweiolkju" , "oxdfgujkmi" ];
let N = str.length;
document.write(good_pairs(str, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(32)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...