Detailed explanation of why (len - pos) * (pos + 1) works
\nLet us understand this problem with an example\nlet word = "abei"\n\nAll possible substrings:\n\na        b       e      i\nab       be      ei\nabe      bei\
<br>\nLet us understand this problem with an example\n<br>let word = "abei"\n\n**All possible substrings:**\n```\na        b       e      i\nab       be      ei\nabe      bei\nabei\n```\n\nSo for counting occurences of vowels in each substring, we can count the occurence of each vowel in each substring\nIn this example, count of vowels in substrings are\n```\na - 4 times\ne - 6 times\ni - 4 times\n```\nAnd you can observe that occurence of each vowel depends on it\'s position like below:\n* a is at position 0 so it can be present only in the substrings starting at 0\n* e is in the middle so it can be present in the substrings starting at it\'s position, substrings ending at it\'s position, and substrings containing it in middle\n* i is at last so it can be present only in the substrings ending at that position\n<br>\n\n**Did you see any pattern ?** Yes !\n<br>A character at position pos can be present in substrings starting at i and substrings ending at j, <br>where **0 <= i <= pos** (pos + 1 choices) and **pos <= j <= len** (len - pos choices) <br>so there are total **(len - pos) * (pos + 1)** substrings that contain the character word[pos]\n\n(You can see from above example that e is at position 2 and it\'s present in substrings "abei", "bei", "ei", "abe", "be", "e"<br> (0 <= start <= pos and pos <= end <= len) and same will be true for each vowel.\n<br>\n**From this observation we can generalise the occurence of each vowel in the string as**\n```\nhere len(abei) = 4\na,  pos = 0, count = (4 - 0) * (0 + 1) = 4\ne,  pos = 2, count = (4 - 2) * (2 + 1) = 6\ni,  pos = 3, count = (4 - 3) * (3 + 1) = 4\n```\n**So the generalised formula will be**\n```\ncount = (len - pos) * (pos + 1)\n```\nand we can keep summing the count of each vowel (don\'t forget to convert to long)\n<br>\n\n\n**C++ code:**\n\n```\nclass Solution {\npublic:\n    bool isVowel(char ch) {\n        return ch == \'a\' or ch == \'e\' or ch == \'i\' or ch == \'o\' or ch == \'u\';\n    }\n    \n    long long countVowels(string word) {\n        long long count = 0;\n        int len = word.size();\n        \n        for(int pos = 0; pos < len; pos++) {\n            if(isVowel(word[pos])) {\n                count += (long)(len - pos) * (long)(pos + 1);\n            }\n        }\n        \n        return count;\n    }\n};\n```\n\n<br>\n\n**Python Code:**\n```\nclass Solution:\n    def countVowels(self, word: str) -> int:\n        count = 0\n        sz = len(word)\n        \n        for pos in range(sz):\n            if word[pos] in \'aeiou\':\n                count += (sz - pos) * (pos + 1)\n        \n        return count\n```\n\n<br>\n\n**Java Code:**\n\n```\nclass Solution {\n    \n    boolean isVowel(char ch) {\n        return ch == \'a\' || ch == \'e\' || ch == \'i\' || ch == \'o\' || ch == \'u\';\n    }\n    \n    public long countVowels(String word) {\n        long count = 0;\n        int len = word.length();\n        \n        for(int pos = 0; pos < len; pos++) {\n            if(isVowel(word.charAt(pos))) {\n                count += (long)(len - pos) * (long)(pos + 1);\n            }\n        }\n        \n        return count;\n    }\n}\n```\n\n<br>\nDo upvote if you liked the explanation :)\n\n<br>
['C', 'Python', 'C++', 'Java', 'Python3']