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