Count substrings that use only vowels and contain all five

0
Sign in

Given a string s of lowercase English letters, return the number of contiguous substrings that satisfy both rules:

  1. Every character of the substring is a vowel (a, e, i, o, u).
  2. The substring contains each of the five vowels at least once.

Substrings are counted by position: two equal substrings that start at different indices both count.

Implement:

def count_vowel_substrings(s: str) -> int: ...

Constraints

  • 0 <= len(s) <= 10^5
  • s contains only lowercase English letters.
  • The answer can exceed 32 bits.

Start with a working solution, then aim for linear time.

Example 1

Input:  s = "aaeiou"
Output: 2

The valid substrings are "aaeiou" and "aeiou".

Example 2

Input:  s = "aeioxuaei"
Output: 0

The x splits the vowels into "aeio" and "uaei", and neither part contains all five vowels.

SnowflakeCodingMedium

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.