LeetCode每日一题——2586. Count the Number of Vowel Strings in Range

文章目录

一、题目

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

二、题解

cpp 复制代码
class Solution {
public:
    int vowelStrings(vector<string>& words, int left, int right) {
        unordered_set<char> set = {'a','e','i','o','u'};
        int res = 0;
        for(int i = left;i <= right;i++){
            char front = words[i][0];
            char back = words[i].back();
            if(set.count(front) && set.count(back)) res++;
        }
        return res;
    }
};
相关推荐
一个专注写代码的程序媛5 分钟前
流式读取数据
java·数据结构·算法
Halo_tjn7 分钟前
Java Set集合知识点
java·开发语言·数据结构·windows·算法
郝学胜-神的一滴8 分钟前
Linux多线程编程:深入理解pthread_cancel函数
linux·服务器·开发语言·c++·软件工程
Trouvaille ~11 分钟前
【C++篇】让错误被温柔对待(下):异常高级特性与最佳实践
运维·开发语言·c++·异常·raii·编程实践·基础入门
-Xie-14 分钟前
Redis(十六)——底层数据结构(一)
java·数据结构·redis
小园子的小菜14 分钟前
深入理解Trie树:敏感词过滤的核心原理与实现思路
算法
Tisfy15 分钟前
LeetCode 2402.会议室 III:优先队列大模拟
算法·leetcode·题解·优先队列·排序·大模拟
byzh_rc18 分钟前
[算法设计与分析-从入门到入土] 基础算法
数据结构·算法·排序算法
2022.11.7始学前端20 分钟前
Dify第二节:AI 出题助手并写入飞书云文档
算法·工作流·dify
小无名呀22 分钟前
使用C语言连接MySQL
数据库·c++·mysql