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;
    }
};
相关推荐
~~李木子~~几秒前
贪心算法实验1
算法·ios·贪心算法
上去我就QWER25 分钟前
C++标准库中的排序算法
c++·排序算法
·云扬·26 分钟前
【LeetCode Hot 100】 136. 只出现一次的数字
算法·leetcode·职场和发展
Xiaochen_1228 分钟前
有边数限制的最短路:Bellman-Ford 算法
c语言·数据结构·c++·程序人生·算法·学习方法·最简单的算法理解
AA陈超42 分钟前
ASC学习笔记0019:返回给定游戏属性的当前值,如果未找到该属性则返回零。
c++·笔记·学习·游戏·ue5·虚幻引擎
阿沁QWQ44 分钟前
HTTP cookie 与 session
c++·浏览器·edge浏览器·cookie·session
熬了夜的程序员2 小时前
【LeetCode】114. 二叉树展开为链表
leetcode·链表·深度优先
铅笔小新z3 小时前
C++入门指南:开启你的编程之旅
开发语言·c++
大胆飞猪6 小时前
递归、剪枝、回溯算法---全排列、子集问题(力扣.46,78)
算法·leetcode·剪枝
君不见,青丝成雪6 小时前
网关整合验签
大数据·数据结构·docker·微服务·系统架构