Count substrings that contain all vowels | SET 2 | C ++ implementation of the approach ; Function that returns true if c is a vowel ; Function to return the count of sub - strings that contain every vowel at least once and no consonant ; Map is used to store count of each vowel ; Start index is set to 0 initially ; If substring till now have all vowels atleast once increment start index until there are all vowels present between ( start , i ) and add n - i each time ; Function to extract all maximum length sub - strings in s that contain only vowels and then calls the countSubstringsUtil ( ) to find the count of valid sub - strings in that string ; If current character is a vowel then append it to the temp string ; The sub - string containing all vowels ends here ; If there was a valid sub - string ; Reset temp string ; For the last valid sub - string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } int countSubstringsUtil ( string s ) { int count = 0 ; map < char , int > mp ; int n = s . length ( ) ; int start = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ s [ i ] ] ++ ; while ( mp [ ' a ' ] > 0 && mp [ ' e ' ] > 0 && mp [ ' i ' ] > 0 && mp [ ' o ' ] > 0 && mp [ ' u ' ] > 0 ) { count += n - i ; mp [ s [ start ] ] -- ; start ++ ; } } return count ; } int countSubstrings ( string s ) { int count = 0 ; string temp = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s [ i ] ) ) { temp += s [ i ] ; } else { if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp ) ; temp = " " ; } } if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp ) ; return count ; } int main ( ) { string s = " aeouisddaaeeiouua " ; cout << countSubstrings ( s ) << endl ; return 0 ; }