dengch

 

统计范围内的元音字符串数

题目概述:给你一个下标从 0 开始的字符串数组 words 和两个整数:left 和 right 。

如果字符串以元音字母开头并以元音字母结尾,那么该字符串就是一个 元音字符串 ,其中元音字母是 'a'、'e'、'i'、'o'、'u' 。

返回 words[i] 是元音字符串的数目,其中 i 在闭区间 [left, right] 内。
解题思路:直接按题意模拟即可
时间复杂度\(O(n)\)
代码

class Solution {
    public int vowelStrings(String[] words, int left, int right) {
        String yuanyin = "aeiou";
        int res = 0;
        for(int i = left; i <= right; i ++){
            int len = words[i].length() - 1;
            if(yuanyin.contains(words[i].charAt(0)+"") && yuanyin.contains(words[i].charAt(len)+""))res++;
        }

        return res;
    }
}

posted on 2023-11-07 20:12  BkDench  阅读(5)  评论(0编辑  收藏  举报

导航