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;
    }
};
相关推荐
笑衬人心。3 分钟前
HTTPS详解:原理 + 加解密过程 + 面试问答
java·网络协议·http·面试·https
蓝澈11215 分钟前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划
zl_dfq7 分钟前
数据结构 之 【堆】(堆的概念及结构、大根堆的实现、向上调整法、向下调整法)(C语言实现)
数据结构
127_127_12710 分钟前
2025 FJCPC 复建 VP
数据结构·图论·模拟·ad-hoc·分治·转化
闪电麦坤9513 分钟前
数据结构:二维数组(2D Arrays)
数据结构·算法
mrbone1117 分钟前
Git-git worktree的使用
开发语言·c++·git·cmake·worktree·gitab
欧阳有财24 分钟前
[java八股文][Mysql面试篇]日志
java·mysql·面试
凌肖战26 分钟前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode
埃菲尔铁塔_CV算法1 小时前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
虾球xz2 小时前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习