Easiest C++ solution O(n)


#1
int Solution::solve(string sub) {
    long long int len  = sub.length();
    long long int sum =0;
    for(int i=0;i<len;i++){
        if(sub[i]=='a' || sub[i]== 'e' ||sub[i]== 'i'||sub[i]== 'o'||sub[i]== 'u'||sub[i]== 'A'||sub[i]== 'E'||sub[i]== 'I'||sub[i]== 'O'||sub[i]== 'U')
                    sum+=len-i;
    
    }
    return sum%10003;
}

#2

Bro, What is the meaning of sum+=len-i ?


#3

for the string in the example test case “ABEC”
the length is 4 and indexing is 0 based. So we have something like this

A B E C
0 1 2 3

now is the subsets are
1. A
2. AB
3. ABE
4. ABEC
5. BE
6. BEC
7. EC
8. C

so a total of 8 subsets but we only need the ones starting with vowels. so what we do is for every character in the input string we check if it is a vowel or not. if it is a vowel we count the number of substrings it will contribute to our final answer. the number of substrings is given by the formula
length of string - index of vowel

so for example for the letter A you have 4-0 number of substrings and you add that to your sum or count and similarly for letter E you have 4-2 number of substrings and you add this to yuor count too. therefore you get the final answer as 6. I hope this clears your doubt.


#4

Thanks Bro . This solution really helps me a lot.


#5

What will happen if a vowel comes again like “A”, in that case the number of substring should be
len - i - 1. As we have already considered “A” in the substrings. So what i did was to store the count also of vowels in a map but apparently the answer I get by that is wrong. Can you help?


#6

will not work foe string like “aaa”.


#7

why %100003 is used?