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

This commit is contained in:
huangge1199@hotmail.com 2021-11-07 14:43:03 +08:00
parent 49cc385f6f
commit 103f277312
2 changed files with 159 additions and 0 deletions

View File

@ -0,0 +1,102 @@
//子字符串 是字符串中的一个连续非空的字符序列
//
// 元音子字符串 由元音'a''e''i''o' 'u'组成的一个子字符串且必须包含 全部五种 元音
//
// 给你一个字符串 word 统计并返回 word 元音子字符串的数目
//
//
//
// 示例 1
//
//
//输入word = "aeiouu"
//输出2
//解释下面列出 word 中的元音子字符串斜体加粗部分
//- "aeiouu"
//- "aeiouu"
//
//
// 示例 2
//
//
//输入word = "unicornarihan"
//输出0
//解释word 中不含 5 种元音所以也不会存在元音子字符串
//
//
// 示例 3
//
//
//输入word = "cuaieuouac"
//输出7
//解释下面列出 word 中的元音子字符串斜体加粗部分
//- "cuaieuouac"
//- "cuaieuouac"
//- "cuaieuouac"
//- "cuaieuouac"
//- "cuaieuouac"
//- "cuaieuouac"
//- "cuaieuouac"
//
// 示例 4
//
//
//输入word = "bbaeixoubb"
//输出0
//解释所有包含全部五种元音的子字符串都含有辅音所以不存在元音子字符串
//
//
//
//
// 提示
//
//
// 1 <= word.length <= 100
// word 仅由小写英文字母组成
//
// 👍 0 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.List;
//5918:统计字符串中的元音子字符串
class CountVowelSubstringsOfAString {
public static void main(String[] args) {
//测试代码
Solution solution = new CountVowelSubstringsOfAString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int countVowelSubstrings(String word) {
int sum = 0;
char[] chs = word.toCharArray();
for (int i = 0; i < word.length(); i++) {
sum += dfs(chs, i, Arrays.asList('a', 'e', 'i', 'o', 'u'), 0, new int[5], 0);
}
return sum;
}
private int dfs(char[] chs, int index, List<Character> list, int sum, int[] arrs, int count) {
if (index == chs.length) {
return sum;
}
if (!list.contains(chs[index])) {
return count == 5 ? sum : 0;
}
if (count < 5 && arrs[list.indexOf(chs[index])] == 0) {
count++;
}
arrs[list.indexOf(chs[index])]++;
if (count == 5) {
sum += 1;
}
return dfs(chs, index + 1, list, sum, arrs, count);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,57 @@
<p><strong>子字符串</strong> 是字符串中的一个连续(非空)的字符序列。</p>
<p><strong>元音子字符串</strong><strong></strong> 由元音(<code>'a'</code><code>'e'</code><code>'i'</code><code>'o'</code><code>'u'</code>)组成的一个子字符串,且必须包含 <strong>全部五种</strong> 元音。</p>
<p>给你一个字符串 <code>word</code> ,统计并返回 <code>word</code><strong>元音子字符串的数目</strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>word = "aeiouu"
<strong>输出:</strong>2
<strong>解释:</strong>下面列出 word 中的元音子字符串(斜体加粗部分):
- "<em><strong>aeiou</strong></em>u"
- "<strong><em>aeiouu</em></strong>"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>word = "unicornarihan"
<strong>输出:</strong>0
<strong>解释:</strong>word 中不含 5 种元音,所以也不会存在元音子字符串。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>word = "cuaieuouac"
<strong>输出:</strong>7
<strong>解释:</strong>下面列出 word 中的元音子字符串(斜体加粗部分):
- "c<em><strong>uaieuo</strong></em>uac"
- "c<em><strong>uaieuou</strong></em>ac"
- "c<em><strong>uaieuoua</strong></em>c"
- "cu<em><strong>aieuo</strong></em>uac"
- "cu<em><strong>aieuou</strong></em>ac"
- "cu<em><strong>aieuoua</strong></em>c"
- "cua<em><strong>ieuoua</strong></em>c"</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>word = "bbaeixoubb"
<strong>输出:</strong>0
<strong>解释:</strong>所有包含全部五种元音的子字符串都含有辅音,所以不存在元音子字符串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= word.length &lt;= 100</code></li>
<li><code>word</code> 仅由小写英文字母组成</li>
</ul>
<div><li>👍 0</li><li>👎 0</li></div>