0 like 0 dislike
1,351 views
String consists of small case letters from 'a' to 'z' ..

Good string is a string which has atleast one vowel & one consonant :)

1<=N<=100000

Expected solutions :

1)O(N^2)

2)O(N) Optimized
in Algorithms and Problems by Expert (91,670 points)
edited by | 1,351 views

1 Answer

0 like 0 dislike
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ; 

int main() {
    
    
    string s ; 
    cin>>s ; 
    ll consonants = -1; 
    ll vowels = -1;
    
    
    ll n = s.size();
    
    
    ll i = 0 ; 
    ll ans = 0 ; 
    while(i<n)
    {
        
        if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
            if(consonants>=0){
           ans+= (consonants+1) ; }
        }
        else
        {
            if(vowels>=0){
          ans+= (vowels+1) ; }
        }
        
        
        
        if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
            vowels = i ; 
        }
        else
        {
            consonants = i ; 
        }
        
        i++;
    }
    cout<<ans; 
    return 0;
    
}
by Expert (91,670 points)