Ctrl K

Vowels Across All Pieces

Medium
You get a string word. Consider all of its non-empty substrings (contiguous pieces), each pair of start and end positions separately, even if two pieces happen to read the same. Count the vowels (a, e, i, o, u) in each substring and return the total over all substrings. The answer can be very large, and enumerating every substring of a long string is too slow.
Example 1
word='oak' → 7

The substrings are o, oa, oak, a, ak, k, with 1 + 2 + 2 + 1 + 1 + 0 = 7 vowels in total.

Example 2
word='xyz' → 0

No substring contains a vowel.

Example 3
word='banana' → 28
Constraints
  • 1 ≤ len(word) ≤ 10⁵
  • word consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBalanced Digit Shuffles HardCollapsing by Counting Ones HardCounting Grid Routes MediumEvery Bracketing Result MediumNobody Gets Their Own Gift Medium
loading Python…
Input
word='oak'
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 82 tests on the server, hidden ones included.