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;
    }
};
相关推荐
:mnong4 分钟前
油藏数值模型ReservoirSim 系统设计分析
c++
计算机安禾5 分钟前
【数据结构与算法】第13篇:栈(三):中缀表达式转后缀表达式及计算
c语言·开发语言·数据结构·c++·算法·链表
another heaven8 分钟前
【软考 IDEF系列方法:从概念到核心差异】
数据结构
Cosolar9 分钟前
AgentScope-Java ReActAgent 代码实现讲解
人工智能·后端·面试
章鱼丸-23 分钟前
DAY40 训练与测试规范写法
人工智能·算法·机器学习
简单~29 分钟前
C++ 函数模板完全指南
c++·函数模板
·心猿意码·30 分钟前
C++ 线程安全单例模式的底层源码级解析
c++·单例模式
代码飞天31 分钟前
算法与数据结构之又臭又长的表
数据结构·算法
A923A36 分钟前
【洛谷刷题 | 第七天】
算法·模拟·洛谷
故事和你9136 分钟前
洛谷-入门4-数组3
开发语言·数据结构·c++·算法·动态规划·图论