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;
    }
};
相关推荐
鹿角片ljp2 分钟前
力扣7.整数反转-从基础到边界条件
算法·leetcode·职场和发展
java修仙传5 分钟前
力扣hot100:前K个高频元素
算法·leetcode·职场和发展
嗷嗷哦润橘_23 分钟前
从萝卜纸巾猫到桌游:“蒸蚌大开门”的设计平衡之旅
人工智能·算法·游戏·概率论·桌游
XH华32 分钟前
数据结构第九章:树的学习(上)
数据结构·学习
xiaoye-duck32 分钟前
吃透C++类和对象(下):内部类、匿名对象及编译器优化的深度解析
c++
TracyCoder12342 分钟前
Java String:从内存模型到不可变设计
java·算法·string
情缘晓梦.1 小时前
C++ 内存管理
开发语言·jvm·c++
黄晓琪1 小时前
Java AQS底层原理:面试深度解析(附实战避坑)
java·开发语言·面试
我是大咖1 小时前
二维数组与数组指针
java·数据结构·算法
筵陌1 小时前
算法:动态规划
算法·动态规划