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

原题链接:https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/description/?envType=daily-question&envId=2023-11-07

C++代码

class Solution {
public:
    bool isvowel(char ch){
        const char vowel[5] = {'a','e','i','o','u' };
        for(int i=0;i<5;i++){
            if(ch==vowel[i]) return true;
        }
        return false;
    }
    int vowelStrings(vector& words, int left, int right) {
        int count = 0;
        for(int i=left;i<=right;i++){
            if(isvowel(words[i][0]) && isvowel(words[i][words[i].size()-1]))
                count++;
        }
        return count;
    }
};

Python代码

class Solution:
    def isvowel(self,ch):
        vowel = ['a','e','i','o','u' ]
        for i in range(5):
            if ch == vowel[i]:
                return True
        return False
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        count = 0
        for i in range(left,right+1):
            if self.isvowel(words[i][0]) and self.isvowel(words[i][-1]):
                count+=1
        return count

你可能感兴趣的:(LeetCode,leetcode,算法)