# Count pairs of strings that satisfy the given conditions

Given an array **arr[]** of **N** strings consisting of lowercase characters, the task is to count the pairs in the array which satisfy the given conditions:

- Both the strings have equal number of pairs.
- The first vowels of both the strings are same.
- The last vowel of both the strings are same.

**Note** that a string can only be used in a single pair.

**Examples:**

Input:arr[] = {“geeks”, “for”, “geeks”, “geek”}

Output:1

The only valid pair is (“geeks”, “geeks”).

“geek” could also be paired with “geeks” but

both the “geeks” have already been paired.

Input:arr[] = {“code”, “shoot”, “mode”}

Output:1

**Approach:** We will store all the vowels that appear in a word for each word and we make a tuple of first vowel, last vowel and the total count of vowels and store corresponding index regarding that tuple using map. At last, we will go through the map and count number of pairs that can be formed using the tuple values stored in the map.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `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<tuple<` `char` `, ` `char` `, ` `int` `>, vector<` `int` `> > map; ` ` ` ` ` `// For every string of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Vector to store the vowels ` ` ` `// of the current string ` ` ` `vector<` `char` `> 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<` `int` `> 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count of pairs of strings which differ in exactly one position
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Number of strings that satisfy the given condition
- Count of sub-sequences which satisfy the given condition
- Pairs of complete strings in two sets of strings
- Count of strings that become equal to one of the two strings after one removal
- Character pairs from two strings with even sum
- Print all pairs of anagrams in a given array of strings
- Pairs of strings which on concatenating contains each character of "string"
- Minimum number of pairs required to make two strings same
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count pairs in an array that hold i*arr[i] > j*arr[j]

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.