Skip to content

Latest commit

 

History

History
112 lines (83 loc) · 2.9 KB

File metadata and controls

112 lines (83 loc) · 2.9 KB

中文文档

Description

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.

Solutions

Python3

Java

TypeScript

function countVowelSubstrings(word: string): number {
    const n = word.length;
    let left = 0,
        right = 0;
    let ans = 0;
    while (right < n) {
        if (!isVowel(word.charAt(right))) {
            // 移动左指针
            left = right + 1;
        } else {
            let cur = word.substring(left, right + 1).split('');
            while (cur.length > 0) {
                if (isValiedArr(cur)) {
                    ans++;
                }
                cur.shift();
            }
        }
        right++;
    }
    return ans;
}

function isVowel(char: string): boolean {
    return ['a', 'e', 'i', 'o', 'u'].includes(char);
}

function isValiedArr(arr: Array<string>): boolean {
    return new Set(arr).size == 5;
}

...