统计字符串中的元音子字符串

子字符串 是字符串中的一个连续(非空)的字符序列。
元音子字符串 是 仅 由元音(’a’、’e’、’i’、’o’ 和 ‘u’)组成的一个子字符串,且必须包含 全部五种 元音。
给你一个字符串 word ,统计并返回 word 中 元音子字符串的数目 。

  • 输入:word = “aeiouu”
    输出:2
    解释:下面列出 word 中的元音子字符串(斜体加粗部分): -aeiouu

  • aeiouu

  • 输入:word = “unicornarihan”
    输出:0
    解释:word 中不含 5 种元音,所以也不会存在元音子字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var countVowelSubstrings = function(word) {
let str = 'aeiou';
let tmp = '';
let deo = [];
for (let i = 0, n = word.length; i < n; i++) {
if (str.includes(word[i])) {
tmp += word[i];
} else {
deo.push(tmp);
tmp = '';
}
}
deo.push(tmp);
deo = deo.filter((e) => e.length >= 5);
if (deo.length == 0) {
return 0;
}
let count = 0;
for (let m = 0; m < deo.length; m++) {
let mon = deo[m];
for (let i = 0, n = mon.length; i < n; i++) {
for (let j = i + 4; j < n; j++) {
let ans = mon.slice(i, j + 1);
if (
Array.from(new Set(ans.split('')))
.sort()
.join('') === str
) {
count++;
}
}
}
}
return count;
};