leetcode
leetcode 2201 ~ 2250
统计范围内的元音字符串数

统计范围内的元音字符串数

难度:

标签:

题目描述

You are given a 0-indexed array of string words and two integers left and right.

A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'.

Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].

 

Example 1:

Input: words = ["are","amy","u"], left = 0, right = 2
Output: 2
Explanation: 
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
- "amy" is not a vowel string because it does not end with a vowel.
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
The number of vowel strings in the mentioned range is 2.

Example 2:

Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
Output: 3
Explanation: 
- "aeo" is a vowel string because it starts with 'a' and ends with 'o'.
- "mu" is not a vowel string because it does not start with a vowel.
- "ooo" is a vowel string because it starts with 'o' and ends with 'o'.
- "artro" is a vowel string because it starts with 'a' and ends with 'o'.
The number of vowel strings in the mentioned range is 3.

 

Constraints:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 10
  • words[i] consists of only lowercase English letters.
  • 0 <= left <= right < words.length

代码结果

运行时间: 24 ms, 内存: 16.6 MB


// 思路:
// 1. 使用 Java Stream 处理从 left 到 right 范围内的 words 数组。
// 2. 过滤出以元音字母开头并以元音字母结尾的字符串。
// 3. 统计满足条件的字符串数量并返回。

import java.util.Arrays;

public class VowelStringCounterStream {
    public static int countVowelStrings(String[] words, int left, int right) {
        String vowels = "aeiou";
        return (int) Arrays.stream(words, left, right + 1)
                .filter(word -> vowels.indexOf(word.charAt(0)) != -1 && vowels.indexOf(word.charAt(word.length() - 1)) != -1)
                .count();
    }
    public static void main(String[] args) {
        String[] words1 = {"are", "amy", "u"};
        int left1 = 0, right1 = 2;
        System.out.println(countVowelStrings(words1, left1, right1)); // 输出:2

        String[] words2 = {"hey", "aeo", "mu", "ooo", "artro"};
        int left2 = 1, right2 = 4;
        System.out.println(countVowelStrings(words2, left2, right2)); // 输出:3
    }
}

解释

方法:

这个题解使用了列表推导和 Python 的内置函数 sum 来统计元音字符串的数量。首先,对于 words 数组中的索引从 left 到 right 的每个元素(即字符串),检查该字符串的第一个字符和最后一个字符是否都是元音字母('a', 'e', 'i', 'o', 'u')。如果两个条件都满足,则该字符串被认为是元音字符串。列表推导会为每个元音字符串生成 True,非元音字符串生成 False。最后,使用 sum 函数计算列表中 True 的总数,即为所求的元音字符串数量。

时间复杂度:

O(n)

空间复杂度:

O(n)

代码细节讲解

🦆
题解中提到使用列表推导和 sum 函数来统计元音字符串,这种方法在处理大数据量时的效率如何?
该方法的时间复杂度是 O(n),其中 n 是 left 到 right 指定范围内的元素数量。因为它需要遍历每个字符串一次来判断首尾字符是否为元音。在大数据量的情况下,这种线性时间复杂度通常是可接受的,但如果数据极大,列表推导可能会引起较大的内存分配压力,因为它首先生成一个布尔值列表。如果对内存或执行时间有更严格的要求,考虑使用循环和条件累加来减少内存使用或并行处理技术来提高效率。
🦆
在题解中,如果输入的 'words' 数组为空或者 'left' 和 'right' 指定的范围不包含任何元素,这种情况下的输出是什么?
如果输入的 'words' 数组为空,或者 'left' 和 'right' 指定的范围不包含任何元素(例如 'left' 大于 'right' 或者超出数组界限),列表推导将不会有任何元素,因此 sum 函数会在一个空的迭代器上运行。在 Python 中,sum 对空的迭代器返回 0。所以,这种情况下输出将是 0,表示没有元音字符串。
🦆
题解考虑了字符串首尾字符是元音字母的情况,但是如果字符串长度为0,即空字符串,这种情况是否会导致错误?
是的,如果字符串长度为 0(即空字符串),尝试访问 s[0] 或 s[-1] 将引发 IndexError,因为这些索引在空字符串中是不存在的。题解中没有明确处理这种情况,因此在实际应用中需要添加对字符串长度的检查,确保在访问首尾字符前字符串不为空。
🦆
题解中使用了 's[0] in "aeiou" and s[-1] in "aeiou"' 来判断元音字符串,为什么不直接创建一个元音集合来进行查找是否更高效?
实际上,使用集合来进行成员查找通常会更高效,因为集合(set)的平均时间复杂度为 O(1) 对于成员检查,相比于字符串或列表的 O(n)。使用集合可以提高判断字符是否为元音的效率,特别是在字符集比较大或查找操作非常频繁的情况下。因此,将 'aeiou' 定义为一个集合,然后进行成员检查,是一个优化的选择。

相关问题