[C++] Simple Observation : O(N) - Pictorially explained
Hello, please upvote the answer if you found it helpful.\n\nThe main idea is to store the number of vowels we count at each index and then use the same count to
Hello, please upvote the answer if you found it helpful.\n\nThe main idea is to store the number of vowels we count at each index and then use the same count to further count the number of vowels for next index. Any character will only contribute to the "prev" variable IF it is a vowel otherwise the number of vowels will remain the same.\nPlease watch the example to get a clear picture of the code.\n![image](https://assets.leetcode.com/users/images/012d8f84-d228-4b9b-8f18-9a827d0eb831_1658948572.1517887.png)\n\n```\nclass Solution {\npublic:\n    long long countVowels(string word) {\n        long long prev=0,sum=0;\n        int n = word.size();\n        for(int j=0;j<n;j++){\n            if(word[j] == \'a\'||  word[j] == \'e\' || word[j] == \'i\' || word[j] == \'o\' || word[j] == \'u\' ) \n                prev += j+1;\n            sum+=prev;\n        }\n        return sum;\n    }\n    \n};\n```
['C']