给定一个由N 个字符串的数组arr[] 。任务是找到所有可能的字符串对的数量,使得它们的串联中的每个元音至少一次。
例子:
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.
朴素的方法:这个想法是从给定的数组中生成所有可能的对,并检查每个可能的两个字符串对的连接是否至少一次包含所有元音。如果是,则将其包括在计数中。在所有操作后打印计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the count of all
// concatenated string with each vowel
// at least once
int good_pair(string str[], int N)
{
int countStr = 0;
// Concatenating all possible
// pairs of string
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
string res = str[i] + str[j];
// Creating an array which checks,
// the presence of each vowel
int vowel[5] = { 0 };
// Checking for each vowel by
// traversing the concatenated
// string
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;
}
// Checking if all the elements
// are set in vowel[]
int temp = 0;
for (int ind = 0; ind < 5; ind++) {
if (vowel[ind] == 1)
temp++;
}
// Check if all vowels are
// present or not
if (temp == 5)
countStr++;
}
}
// Return the final count
return countStr;
}
// Driver Code
int main()
{
// Given array of strings
string arr[] = { "aaweiolkju", "oxdfgujkmi" };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
cout << good_pair(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to return the count of all
// concatenated String with each vowel
// at least once
static int good_pair(String str[], int N)
{
int countStr = 0;
// Concatenating all possible
// pairs of String
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
String res = str[i] + str[j];
// Creating an array which checks,
// the presence of each vowel
int vowel[] = new int[5];
// Checking for each vowel by
// traversing the concatenated
// String
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;
}
// Checking if all the elements
// are set in vowel[]
int temp = 0;
for (int ind = 0; ind < 5; ind++)
{
if (vowel[ind] == 1)
temp++;
}
// Check if all vowels are
// present or not
if (temp == 5)
countStr++;
}
}
// Return the final count
return countStr;
}
// Driver Code
public static void main(String[] args)
{
// Given array of Strings
String arr[] = { "aaweiolkju", "oxdfgujkmi" };
int N = arr.length;
// Function Call
System.out.print(good_pair(arr, N));
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program for the above approach
# Function to return the count of all
# concatenated string with each vowel
# at least once
def good_pair(st, N):
countStr = 0
# Concatenating all possible
# pairs of string
for i in range(N):
for j in range(i + 1, N):
res = st[i] + st[j]
# Creating an array which checks,
# the presence of each vowel
vowel = [0] * 5
# Checking for each vowel by
# traversing the concatenated
# string
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
# Checking if all the elements
# are set in vowel[]
temp = 0
for ind in range(5):
if (vowel[ind] == 1):
temp += 1
# Check if all vowels are
# present or not
if (temp == 5):
countStr += 1
# Return the final count
return countStr
# Driver Code
if __name__ == "__main__":
# Given array of strings
arr = [ "aaweiolkju", "oxdfgujkmi" ]
N = len(arr)
# Function call
print(good_pair(arr, N))
# This code is contributed by jana_sayantan
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the count of all
// concatenated String with each vowel
// at least once
static int good_pair(String []str, int N)
{
int countStr = 0;
// Concatenating all possible
// pairs of String
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
String res = str[i] + str[j];
// Creating an array which checks,
// the presence of each vowel
int []vowel = new int[5];
// Checking for each vowel by
// traversing the concatenated
// String
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;
}
// Checking if all the elements
// are set in vowel[]
int temp = 0;
for(int ind = 0; ind < 5; ind++)
{
if (vowel[ind] == 1)
temp++;
}
// Check if all vowels are
// present or not
if (temp == 5)
countStr++;
}
}
// Return the readonly count
return countStr;
}
// Driver Code
public static void Main(String[] args)
{
// Given array of Strings
String []arr = { "aaweiolkju", "oxdfgujkmi" };
int N = arr.Length;
// Function call
Console.Write(good_pair(arr, N));
}
}
// This code is contributed by Princi Singh
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the count of all
// concatenated string with each vowel
// at least once
int good_pairs(string str[], int N)
{
// Creating a hash array with
// initial value as 0
int arr[32] = { 0 }, strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for (int i = 0; i < N; i++) {
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for (int i = 0; i < 32; i++) {
for (int j = i + 1; j < 32; j++) {
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver Code
int main()
{
// Given array of strings
string str[] = { "aaweiolkju", "oxdfgujkmi" };
int N = sizeof(str) / sizeof(str[0]);
// Function Call
cout << good_pairs(str, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to return the count of all
// concatenated string with each vowel
// at least once
public static int good_pairs(String[] str,
int N)
{
// Creating a hash array with
// initial value as 0
int arr[] = new int[32];
int strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for(int i = 0; i < N; i++)
{
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for(int i = 0; i < 32; i++)
{
for(int j = i + 1; j < 32; j++)
{
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver code
public static void main(String[] args)
{
// Given array of strings
String str[] = { "aaweiolkju", "oxdfgujkmi" };
int N = str.length;
// Function call
System.out.println(good_pairs(str, N));
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
# Function to return the count of all
# concatenated string with each vowel
# at least once
def good_pairs(Str, N):
# Creating a hash array with
# initial value as 0
arr = [0 for i in range(32)]
strCount = 0
# Traversing through each string
# and getting hash value for each
# of them
for i in range(N):
# Initializing the weight
# of each string
Weight = 0
# Find the hash value for
# each string
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)
# Increasing the count
# of the hash value
arr[Weight] += 1
# Getting all possible pairs
# of indexes in hash array
for i in range(32):
for j in range(i + 1, 32):
# Check if the pair which has
# hash value 31 and
# multiplying the count of
# string and add it strCount
if ((i | j) == 31):
strCount += arr[i] * arr[j]
# Corner case, for strings which
# independently has all the vowels
strCount += int((arr[31] * (arr[31] - 1)) / 2)
# Return thre final count
return strCount
# Driver Code
# Given array of strings
Str = [ "aaweiolkju", "oxdfgujkmi" ]
N = len(Str)
print(good_pairs(Str, N))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the count of all
// concatenated string with each vowel
// at least once
public static int good_pairs(string[] str,
int N)
{
// Creating a hash array with
// initial value as 0
int[] arr = new int[32];
int strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for(int i = 0; i < N; i++)
{
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for(int i = 0; i < 32; i++)
{
for(int j = i + 1; j < 32; j++)
{
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver code
public static void Main(string[] args)
{
// Given array of strings
string[] str = { "aaweiolkju", "oxdfgujkmi" };
int N = str.Length;
// Function call
Console.Write(good_pairs(str, N));
}
}
// This code is contributed by rock_cool
Javascript
1
时间复杂度: O(N 4 )
辅助空间: O(N)
高效的方法:优化上述方法的想法是使用哈希。以下是步骤:
- 创建一个长度为 32 的哈希hash[] ,所有元素都为0 。
- 通过采用变量权重并在遍历每个字符串的同时对该变量执行按位 OR 运算以2 的幂来使用散列:
for 'a': Weight | 1,
for 'e': Weight | 2,
for 'i': Weight | 4,
for 'o': Weight | 8,
for 'u': Weight | 16
- weight的值是一个哈希值,它将指示字符串包含的元音组合。
例如:
"aeiouau": Weight = 31, as it has all the vowels and
"oaiie": Weight = 15, as it has all the vowels except 'u'
- 将hash[] 中权重表示的索引增加1 ,元素是字符串的计数,索引作为它们的哈希值。
- 因此,索引(i, j) 的所有哈希值对都是按位 OR为31的所有元音对,它们至少包含一次。让这样的字符串的计数为countStr 。因此,计数由下式给出:
countStr = hash[i] * hash[j] - 使用上述步骤中的公式添加每对的计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the count of all
// concatenated string with each vowel
// at least once
int good_pairs(string str[], int N)
{
// Creating a hash array with
// initial value as 0
int arr[32] = { 0 }, strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for (int i = 0; i < N; i++) {
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for (int i = 0; i < 32; i++) {
for (int j = i + 1; j < 32; j++) {
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver Code
int main()
{
// Given array of strings
string str[] = { "aaweiolkju", "oxdfgujkmi" };
int N = sizeof(str) / sizeof(str[0]);
// Function Call
cout << good_pairs(str, N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to return the count of all
// concatenated string with each vowel
// at least once
public static int good_pairs(String[] str,
int N)
{
// Creating a hash array with
// initial value as 0
int arr[] = new int[32];
int strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for(int i = 0; i < N; i++)
{
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for(int i = 0; i < 32; i++)
{
for(int j = i + 1; j < 32; j++)
{
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver code
public static void main(String[] args)
{
// Given array of strings
String str[] = { "aaweiolkju", "oxdfgujkmi" };
int N = str.length;
// Function call
System.out.println(good_pairs(str, N));
}
}
// This code is contributed by divyeshrabadiya07
蟒蛇3
# Python3 program for the above approach
# Function to return the count of all
# concatenated string with each vowel
# at least once
def good_pairs(Str, N):
# Creating a hash array with
# initial value as 0
arr = [0 for i in range(32)]
strCount = 0
# Traversing through each string
# and getting hash value for each
# of them
for i in range(N):
# Initializing the weight
# of each string
Weight = 0
# Find the hash value for
# each string
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)
# Increasing the count
# of the hash value
arr[Weight] += 1
# Getting all possible pairs
# of indexes in hash array
for i in range(32):
for j in range(i + 1, 32):
# Check if the pair which has
# hash value 31 and
# multiplying the count of
# string and add it strCount
if ((i | j) == 31):
strCount += arr[i] * arr[j]
# Corner case, for strings which
# independently has all the vowels
strCount += int((arr[31] * (arr[31] - 1)) / 2)
# Return thre final count
return strCount
# Driver Code
# Given array of strings
Str = [ "aaweiolkju", "oxdfgujkmi" ]
N = len(Str)
print(good_pairs(Str, N))
# This code is contributed by avanitrachhadiya2155
C#
// C# program for the above approach
using System;
class GFG{
// Function to return the count of all
// concatenated string with each vowel
// at least once
public static int good_pairs(string[] str,
int N)
{
// Creating a hash array with
// initial value as 0
int[] arr = new int[32];
int strCount = 0;
// Traversing through each string
// and getting hash value for each
// of them
for(int i = 0; i < N; i++)
{
// Initializing the weight
// of each string
int Weight = 0;
// Find the hash value for
// each string
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;
}
}
// Increasing the count
// of the hash value
arr[Weight]++;
}
// Getting all possible pairs
// of indexes in hash array
for(int i = 0; i < 32; i++)
{
for(int j = i + 1; j < 32; j++)
{
// Check if the pair which has
// hash value 31 and
// multiplying the count of
// string and add it strCount
if ((i | j) == 31)
strCount += arr[i] * arr[j];
}
}
// Corner case, for strings which
// independently has all the vowels
strCount += (arr[31] * (arr[31] - 1)) / 2;
// Return thre final count
return strCount;
}
// Driver code
public static void Main(string[] args)
{
// Given array of strings
string[] str = { "aaweiolkju", "oxdfgujkmi" };
int N = str.Length;
// Function call
Console.Write(good_pairs(str, N));
}
}
// This code is contributed by rock_cool
Javascript
1
时间复杂度: O(N 2 )
辅助空间: O(32)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live