2062. Count Vowel Substrings of a String

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.

 

Example 1:

Input: word = "aeiouu"
Output: 2
Explanation: The vowel substrings of word are as follows (underlined):
- "aeiouu"
- "aeiouu"

Example 2:

Input: word = "unicornarihan"
Output: 0
Explanation: Not all 5 vowels are present, so there are no vowel substrings.

Example 3:

Input: word = "cuaieuouac"
Output: 7
Explanation: The vowel substrings of word are as follows (underlined):
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"

 

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase English letters only.

2062. Count Vowel Substrings of a String
struct Solution;

impl Solution {
    fn count_vowel_substrings(word: String) -> i32 {
        let s: Vec<char> = word.chars().collect();
        let n = s.len();
        let mut res = 0;
        for i in 0..n {
            for j in i..n {
                if Solution::is_vowel(&s, i, j) {
                    res += 1;
                }
            }
        }
        res
    }

    fn is_vowel(s: &[char], start: usize, end: usize) -> bool {
        let mut ca = 0;
        let mut ce = 0;
        let mut ci = 0;
        let mut co = 0;
        let mut cu = 0;

        for i in start..=end {
            match s[i] {
                'a' => {
                    ca += 1;
                }
                'e' => {
                    ce += 1;
                }
                'i' => {
                    ci += 1;
                }
                'o' => {
                    co += 1;
                }
                'u' => {
                    cu += 1;
                }
                _ => {
                    return false;
                }
            }
        }
        ca >= 1 && ce >= 1 && ci >= 1 && co >= 1 && cu >= 1
    }
}

#[test]
fn test() {
    let word = "aeiouu".to_string();
    let res = 2;
    assert_eq!(Solution::count_vowel_substrings(word), res);
    let word = "unicornarihan".to_string();
    let res = 0;
    assert_eq!(Solution::count_vowel_substrings(word), res);
    let word = "cuaieuouac".to_string();
    let res = 7;
    assert_eq!(Solution::count_vowel_substrings(word), res);
}