leetcode
leetcode 2501 ~ 2550
统计美丽子字符串 I

统计美丽子字符串 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 of vowels and consonants is divisible by k.

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`在设置初始值时的边界条件处理正确?
在题解中,列表`f`用于存储从字符串开始到当前位置的元音字母总数。为正确处理边界条件,应在遍历字符串之前初始化`f`为空列表,并在每次遍历字符时,更新并添加当前元音计数`v`到`f`中。初始元音计数`v`应设置为0,以确保从字符串的第一个字符开始计算时,元音字母的累计是正确的。每次字符判断后,`v`的值需要根据字符是否为元音进行更新,然后将这个更新后的值添加到`f`中。这样,`f`的每个元素`f[j]`就能准确地反映出从字符串开始到位置`j`的元音字母总数。
🦆
这种算法的空间复杂度为什么?而其时间复杂度又是多少?如何优化此算法?
这种算法的空间复杂度主要由元音总数列表`f`决定,为`O(n)`,其中`n`是字符串`s`的长度。时间复杂度为`O(n^2)`,因为对于字符串中的每个位置`j`,算法都需要向前遍历以检查所有可能的子字符串。为优化此算法,可以考虑使用滑动窗口或哈希表来减少不必要的重复计算。例如,可以维护一个窗口内元音和辅音的计数,当窗口滑动时,只更新进入和离开窗口的字符计数,从而将时间复杂度降低到接近`O(n)`。
🦆
如果`i`为0时,如何计算起始位置到`j`的子字符串的元音数和辅音数?
如果起始位置`i`为0,这表示子字符串从字符串`s`的起始位置开始,一直到位置`j`。在这种情况下,元音数直接由`f[j]`给出,因为`f[j]`存储的是从字符串开始到位置`j`的元音字母总数。辅音数则可以通过`j + 1 - f[j]`计算得出,这里`j + 1`是子字符串的总长度(因为索引是从0开始的),`f[j]`是元音的数量,两者的差就是辅音的数量。

相关问题