Count length-5 substrings with a consonant-vowel pattern and distinct letters

Quick Overview

Count every length-5 substring of a lowercase string whose letters follow a consonant, vowel, vowel, consonant, vowel pattern with no repeated letter. It tests fixed-size sliding windows, careful index checks and constant-time distinctness tests.

Count length-5 substrings with a consonant-vowel pattern and distinct letters

Company: Baidu

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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. ```text 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. ```text 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.

Overview: Count every length-5 substring of a lowercase string whose letters follow a consonant, vowel, vowel, consonant, vowel pattern with no repeated letter. It tests fixed-size sliding windows, careful index checks and constant-time distinctness tests.

Baidu logo
Baidu
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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.