Given a string s, count the number of substrings of length exactly 5 that satisfy all of the following conditions:
-
The 1st and 4th characters are consonants.
-
The 2nd, 3rd and 5th characters are vowels (one of
a
,
e
,
i
,
o
,
u
).
-
All five characters are distinct (no character repeats within the substring).
Return the total count of such substrings. Substrings at different starting positions are counted separately even if they contain the same characters.
Function Signature
def count_cvvcv_substrings(s: str) -> int:
Rules
-
Only
a
,
e
,
i
,
o
and
u
are vowels. Every other lowercase letter, including
y
, is a consonant.
-
A substring is contiguous. The required pattern inside each window of 5 is consonant, vowel, vowel, consonant, vowel.
-
If
s
is shorter than 5 characters, there are no windows and the answer is 0.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters only.
Examples
Input: s = "baidu"
Output: 1
The only window is "baidu". Its 1st and 4th letters (b, d) are consonants, its 2nd, 3rd and 5th letters (a, i, u) are vowels, and no letter repeats.
Input: s = "baiduxbeicobaido"
Output: 3
The windows starting at indices 0 ("baidu"), 6 ("beico") and 11 ("baido") match. Each of the other nine windows breaks the consonant-vowel pattern.
Input: s = "baibu"
Output: 0
"baibu" follows the consonant-vowel pattern, but b is both the 1st and the 4th letter, so the five letters are not distinct.