📜  计算满足给定条件的字符串对

📅  最后修改于: 2021-05-08 18:43:01             🧑  作者: Mango

给定由小写字符组成的N个字符串组成的数组arr [] ,任务是计算满足给定条件的数组中的对:

  1. 两个字符串的对数相等。
  2. 两个字符串的第一个元音相同。
  3. 两个字符串的最后一个元音是相同的。

请注意,一个字符串只能成对使用。

例子:

方法:我们将存储每个单词中出现在单词中的所有元音,并制作第一个元音,最后一个元音和元音总数的元组,并使用地图存储关于该元组的相应索引。最后,我们将遍历地图并计算使用地图中存储的元组值可以形成的对数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if c is vowel
bool is_vowel(char c)
{
    return (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u');
}
  
// Function to return the count of required pairs
int count(string s[], int n)
{
  
    map, vector > map;
  
    // For every string of the array
    for (int i = 0; i < n; i++) {
  
        // Vector to store the vowels
        // of the current string
        vector vowel;
        for (int j = 0; j < s[i].size(); j++) {
  
            // If current character is a vowel
            if (is_vowel(s[i][j]))
                vowel.push_back(s[i][j]);
        }
  
        // If current string contains vowels
        if (vowel.size() > 0) {
            int len = vowel.size();
  
            // Create tuple (first vowel,
            // last vowel, total vowels)
            map[make_tuple(vowel[0],
                           vowel[len - 1], len)]
                .push_back(i);
        }
    }
  
    int count = 0;
    for (auto i : map) {
  
        // v stores the indices for which
        // the given condition satisfies
        // Total valid pairs will be half the size
        vector v = i.second;
        count += v.size() / 2;
    }
  
    return count;
}
  
// Driver code
int main()
{
    string s[] = { "geeks", "for", "geeks" };
    int n = sizeof(s) / sizeof(string);
  
    cout << count(s, n);
  
    return 0;
}


Python3
# Python3 implementation of the approach
  
# Function that returns true if c is vowel
def is_vowel(c):
    return (c == 'a' or c == 'e' or c == 'i'
            or c == 'o' or c == 'u')
  
  
# Function to return the count of required pairs
def count(s, n):
  
  
    map=dict()
  
    # For every of the array
    for i in range(n):
  
        # Vector to store the vowels
        # of the current string
        vowel=[]
        for j in range(len(s[i])):
  
            # If current character is a vowel
            if (is_vowel(s[i][j])):
                vowel.append(s[i][j])
      
  
        # If current contains vowels
        if (len(vowel) > 0):
            Len = len(vowel)
  
            # Create tuple (first vowel,
            # last vowel, total vowels)
            if (vowel[0],vowel[Len - 1], Len) in map.keys():
                map[(vowel[0],vowel[Len - 1], Len)].append(i)
            else:
                map[(vowel[0],vowel[Len - 1], Len)]=[i,]
          
  
    count = 0
    for i in map:
  
        # v stores the indices for which
        # the given condition satisfies
        # Total valid pairs will be half the size
        v = map[i]
        count += len(v)// 2
      
  
    return count
  
# Driver code
s = ["geeks", "for", "geeks"]
n = len(s)
  
print(count(s, n))
  
# This code is contributed by mohit kumar 29


输出:
1