Intuition Explained || Calculate Contribution || C++ Clean Code
Intuition : \n\n Idea is to find the contribution of each vowel in substrings which it is part of.\n Thus, either it can part of substrings on its left, or on i
**Intuition :** \n\n* Idea is to find the contribution of each vowel in substrings which it is part of.\n* Thus, either it can part of substrings on its left, or on its right.\n\t* i.e substring ending at current character , or starting at current character.\n\n\t* We can calculate number of substrings between [i..j] = `(j - i + 1)`\n\n* Thus, contibution = (no. of substrings on its left) * (no. of substrings on its right)\n\t* Formula : **`contribution = (i+1) * (n-i)`** , here `i` is index of current vowel character\n* Finally, sum of contibution of each vowel will give us the answer. \n* Lets do a dry run on a TC : \n\t\t\n\t\tEx: word = "a b a"\n\n\t\t0. word[0] = a, which is a vowel \n\t\t\t=> So, substrings in which word[0] is part of = {\'a\', \'a\'b, \'a\'ba}\n\t\t\t=> contibution(word[0]) = (0+1) * (3 - 0) = 3 \n\n\t\t1. word[1] = b, which is not a vowel\n\t\t\n\t\t2. word[2] = a, which is a vowel\n\t\t\t=> So, substrings in which word[2] is part of : {ab\'a\', b\'a\', \'a\'}\n\t\t\t=> contibution(word[2]) = (2 + 1) * (3 - 2) = 3\n\n\t\tTherefore, answer = contibution(word[0]) + contibution(word[1]) + contibution(word[2]) = 3 + 0 + 3 = 6\n\n# Code: \n\n```\nclass Solution {\npublic:\n    \n    bool isVowel(char c) {\n        return (c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\');\n    }\n    \n    long long countVowels(string word) {\n        \n        int n = word.size();\n        long long count = 0;\n        \n        for(int i=0; i<n; i++) {\n            if(isVowel(word[i]))\n                count += (long long)(i+1) * (long long)(n-i);\n        }\n        \n        return count;\n    }\n};\n```\n\n**Complexity :** \n\n* Time : `O(N)` , N : size of word\n* Space : `O(1)`\n\n***If you find this helpful, do give it a like :)***
[]