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; } |
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