Skip to main content
Back to problems
#2062
Easy Algorithms

Count vowel substrings of a string

Hash Table String
72.9% acceptance
Feb 25, 2026
1112
433
A substring is a contiguous (non-empty) sequence of characters within a string. A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it. Given a string word, return the number of vowel substrings in word.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_vowel_substrings(word: String) -> i32 {
    let w: Vec<u8> = word.bytes().collect();
    let n = w.len();
    let is_vowel = |c: u8| matches!(c, b'a' | b'e' | b'i' | b'o' | b'u');
    let mut count = 0;
    for i in 0..n {
      if !is_vowel(w[i]) { continue; }
      let mut freq = [0u32; 5];
      let vowels = [b'a', b'e', b'i', b'o', b'u'];
      for j in i..n {
        if !is_vowel(w[j]) { break; }
        let idx = vowels.iter().position(|&v| v == w[j]).unwrap();
        freq[idx] += 1;
        if freq.iter().all(|&f| f > 0) {
          count += 1;
        }
      }
    }
    count
  }
}