统计美丽子字符串 I
难度:
标签:
题目描述
You are given a string s
and a positive integer k
.
Let vowels
and consonants
be the number of vowels and consonants in a string.
A string is beautiful if:
vowels == consonants
.(vowels * consonants) % k == 0
, in other terms the multiplication ofvowels
andconsonants
is divisible byk
.
Return the number of non-empty beautiful substrings in the given string s
.
A substring is a contiguous sequence of characters in a string.
Vowel letters in English are 'a'
, 'e'
, 'i'
, 'o'
, and 'u'
.
Consonant letters in English are every letter except vowels.
Example 1:
Input: s = "baeyh", k = 2 Output: 2 Explanation: There are 2 beautiful substrings in the given string. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]). You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0. - Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]). You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string.
Example 2:
Input: s = "abba", k = 1 Output: 3 Explanation: There are 3 beautiful substrings in the given string. - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]). - Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]). It can be shown that there are only 3 beautiful substrings in the given string.
Example 3:
Input: s = "bcdf", k = 1 Output: 0 Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 1000
1 <= k <= 1000
s
consists of only English lowercase letters.
代码结果
运行时间: 50 ms, 内存: 16.4 MB
/*
题目思路:
1. 定义元音字母和辅音字母。
2. 使用双重循环遍历所有子字符串。
3. 对每个子字符串计算元音和辅音的数量。
4. 检查子字符串是否为美丽字符串。
5. 统计美丽子字符串的数量。
*/
import java.util.stream.IntStream;
public class BeautifulSubstringsStream {
public long countBeautifulSubstrings(String s, int k) {
String vowels = "aeiou";
return IntStream.range(0, s.length())
.mapToLong(i -> IntStream.range(i + 1, s.length() + 1)
.mapToObj(j -> s.substring(i, j))
.filter(sub -> {
long vowelsCount = sub.chars().filter(c -> vowels.indexOf(c) != -1).count();
long consonantsCount = sub.length() - vowelsCount;
return vowelsCount == consonantsCount && (vowelsCount * consonantsCount) % k == 0;
})
.count())
.sum();
}
public static void main(String[] args) {
BeautifulSubstringsStream solution = new BeautifulSubstringsStream();
System.out.println(solution.countBeautifulSubstrings("baeyh", 2)); // 输出: 2
System.out.println(solution.countBeautifulSubstrings("abba", 1)); // 输出: 3
System.out.println(solution.countBeautifulSubstrings("bcdf", 1)); // 输出: 0
}
}
解释
方法:
该题解采用了一个遍历的方式来寻找所有可能的子字符串,并检查每个子字符串是否满足美丽字符串的条件。它首先使用一个集合来判断字符是否为元音。然后,使用一个列表 `f` 来记录从字符串开始到当前位置的元音字母的数量。对于每个字符位置 `j`,遍历所有可能的起始位置 `i`,计算从 `i` 到 `j` 的子字符串中的元音数和辅音数,进而检查这段子字符串是否满足美丽字符串的定义。通过两层循环遍历所有可能的子字符串,并通过一些简单的数学计算来判断子字符串是否满足条件。
时间复杂度:
O(n^2)
空间复杂度:
O(n)
代码细节讲解
🦆
如何确保用于计算元音字母的总数列表`f`在设置初始值时的边界条件处理正确?
▷🦆
这种算法的空间复杂度为什么?而其时间复杂度又是多少?如何优化此算法?
▷🦆
如果`i`为0时,如何计算起始位置到`j`的子字符串的元音数和辅音数?
▷