Vowels of All Substrings - Problem
Given a string word, return the sum of the number of vowels ('a', 'e', 'i', 'o', and 'u') in every substring of word.
A substring is a contiguous (non-empty) sequence of characters within a string.
Note: Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.
Input & Output
Example 1 — Basic Case
$
Input:
word = "aba"
›
Output:
6
💡 Note:
Substrings are: "a" (1 vowel), "ab" (1 vowel), "aba" (2 vowels), "b" (0 vowels), "ba" (1 vowel), "a" (1 vowel). Total: 1+1+2+0+1+1 = 6
Example 2 — No Vowels
$
Input:
word = "abc"
›
Output:
3
💡 Note:
Substrings are: "a" (1), "ab" (1), "abc" (1), "b" (0), "bc" (0), "c" (0). Total: 1+1+1+0+0+0 = 3
Example 3 — All Vowels
$
Input:
word = "aei"
›
Output:
9
💡 Note:
Each position contributes: a at pos 0: (0+1)*(3-0)=3, e at pos 1: (1+1)*(3-1)=4, i at pos 2: (2+1)*(3-2)=3. Total: 3+4+2 = 9
Constraints
- 1 ≤ word.length ≤ 105
- word consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with vowels and consonants
2
Process
Count vowels across all possible substrings
3
Output
Total sum of all vowel occurrences
Key Takeaway
🎯 Key Insight: Each vowel at position i contributes to (i+1)×(n-i) substrings - use math instead of generating all substrings
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code