Intuitive, easy to understand Solution. time ~ O(n), space ~ O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \nOur first intuition must be a O(n^2) solution which makes the every possible subset, th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOur first intuition must be a O(n^2) solution which makes the every possible subset, then count the total number of vowel in it and then add to the counter.\n\nBut think some reverse look-up here. Instead of thinking as founding the every subset then counting vowel in it.\nThink like for every character how much it contribute to the answer.\n\nfor example, input = "aba",\n- (first a) a comes in substrings "a", "ab", "aba" -> contributes 1 each = 3\n- b comes in substrings "ab", "aba", "b", "ba" -> contribute 0 each = 0\n- (second a) a comes in substrings "aba", "ba", "a" -> contribute 1 each = 3\n\nAdding all give the answer 3 + 0 + 3 = 6.\n\nImportant points :\n- Consonents won\'t add. If consonent appear skip it.\n- If vowel appear, we need to find to find in how many substring it can appear, this can be done in O(1).\n- Two same characters are totally independent of other same characters, so we don\'t have to care about \'does other same character would contribute with current character ?\'\n- Answer can be big here, use "long long int" or any respective bigger data type to not get any overflow.\n\n![Sol.png](https://assets.leetcode.com/users/images/f044f1cc-4bbd-4c72-8a2a-b7a7af57ccb4_1685795143.2351522.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize counter cnt = 0.\n2. Iterate i from 0 to n-1\n    2.1. if word[i] is vowel, then add (i+1) * (n-i) to the cnt.\n3. Return cnt\n\n\n# Complexity\n- Time complexity: $$O(n)$$, as we only iterate for each character\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$, as we only use cnt and i variables, also we are using one function stack space as we are calling one isVowel() function. \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    bool isVowel(char c) {\n        if(c == \'a\' || c == \'e\' || c == \'i\' || c == \'o\' || c == \'u\') return true;\n        return false;\n    }\npublic:\n    long long countVowels(string word) {\n        long long cnt = 0;\n        for(int i=0 ; i<word.size() ; ++i)\n            if(isVowel(word[i])) \n                cnt += (i + 1)*(word.size() - i);\n        return cnt;\n    }\n};\n```
['Math', 'String', 'Combinatorics', 'Counting', 'C++']