📜  计算串联每个元音的字符串对

📅  最后修改于: 2021-05-17 01:58:37             🧑  作者: Mango

给定N个字符串的数组arr [] 。任务是找到所有可能的字符串对的计数,以使它们的串联每个元音至少出现一次。

例子:

天真的方法:这个想法是从给定的数组中生成所有可能的对,并检查每两个可能的字符串对的连接是否至少具有一次元音。如果是,则将其包括在计数中。完成所有操作后打印计数。

下面是上述方法的实现:

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 hash [] ,所有元素均为0
  • 通过取变量权重并使用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)给出的按位或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

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

Java脚本


输出:
1

时间复杂度: O(N 2 )
辅助空间: O(32)