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;
    }
};
相关推荐
鄃鳕34 分钟前
python 字典 列表 类比c++【python】
c++·python
im_AMBER34 分钟前
算法笔记 05
笔记·算法·哈希算法
夏鹏今天学习了吗40 分钟前
【LeetCode热题100(46/100)】从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
吃着火锅x唱着歌41 分钟前
LeetCode 2389.和有限的最长子序列
算法·leetcode·职场和发展
嶔某1 小时前
二叉树的前中后序遍历(迭代)
算法
WWZZ20251 小时前
快速上手大模型:机器学习2(一元线性回归、代价函数、梯度下降法)
人工智能·算法·机器学习·计算机视觉·机器人·大模型·slam
孤狼灬笑1 小时前
深度学习经典分类(算法分析与案例)
rnn·深度学习·算法·cnn·生成模型·fnn
dragoooon341 小时前
[优选算法专题四.前缀和——NO.26二维前缀和]
算法
保持低旋律节奏2 小时前
C++——list链表
c++·链表·list
倔强青铜三2 小时前
苦练Python第69天:subprocess模块从入门到上瘾,手把手教你驯服系统命令!
人工智能·python·面试