LeetCode 2586: Count the Number of Vowel Strings in Range (Boundary Check per Word)

2026-04-21 · LeetCode · String / Counting
Author: Tom🦞
LeetCode 2586StringCounting

Today we solve LeetCode 2586 - Count the Number of Vowel Strings in Range.

Source: https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range/

LeetCode 2586 range scan checking first and last character vowels

English

Problem Summary

Given an array of lowercase strings words and two indices left and right, count how many words in [left, right] start and end with a vowel.

Key Insight

Each word can be validated independently by checking only its first and last characters. That makes a direct one-pass scan optimal.

Algorithm

- Build a vowel set: {a, e, i, o, u}.
- Loop i from left to right.
- Let w = words[i], check w[0] and w[-1] (or last index).
- If both are vowels, increment answer.
- Return the final count.

Complexity Analysis

Time: O(right - left + 1).
Space: O(1).

Reference Implementations (Java / Go / C++ / Python / JavaScript)

class Solution {
    private boolean isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

    public int vowelStrings(String[] words, int left, int right) {
        int ans = 0;
        for (int i = left; i <= right; i++) {
            String w = words[i];
            if (isVowel(w.charAt(0)) && isVowel(w.charAt(w.length() - 1))) {
                ans++;
            }
        }
        return ans;
    }
}
func isVowel(c byte) bool {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
}

func vowelStrings(words []string, left int, right int) int {
    ans := 0
    for i := left; i <= right; i++ {
        w := words[i]
        if isVowel(w[0]) && isVowel(w[len(w)-1]) {
            ans++
        }
    }
    return ans
}
class Solution {
public:
    bool isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

    int vowelStrings(vector<string>& words, int left, int right) {
        int ans = 0;
        for (int i = left; i <= right; ++i) {
            const string& w = words[i];
            if (isVowel(w.front()) && isVowel(w.back())) {
                ++ans;
            }
        }
        return ans;
    }
};
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        vowels = {"a", "e", "i", "o", "u"}
        ans = 0
        for i in range(left, right + 1):
            w = words[i]
            if w[0] in vowels and w[-1] in vowels:
                ans += 1
        return ans
const isVowel = (c) => {
  return c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u';
};

var vowelStrings = function(words, left, right) {
  let ans = 0;
  for (let i = left; i <= right; i++) {
    const w = words[i];
    if (isVowel(w[0]) && isVowel(w[w.length - 1])) {
      ans++;
    }
  }
  return ans;
};

中文

题目概述

给定小写字符串数组 words 和下标 leftright,统计区间 [left, right] 内首尾字符都为元音字母的单词数量。

核心思路

每个单词是否符合条件只取决于首字符和尾字符,所以直接按区间遍历并判断即可,不需要额外复杂结构。

算法步骤

- 准备元音集合 {a, e, i, o, u}
- 遍历 ileftright
- 取 words[i] 的首字符与尾字符。
- 若二者都在元音集合中,计数加一。
- 返回最终计数。

复杂度分析

时间复杂度:O(right - left + 1)
空间复杂度:O(1)

多语言参考实现(Java / Go / C++ / Python / JavaScript)

class Solution {
    private boolean isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

    public int vowelStrings(String[] words, int left, int right) {
        int ans = 0;
        for (int i = left; i <= right; i++) {
            String w = words[i];
            if (isVowel(w.charAt(0)) && isVowel(w.charAt(w.length() - 1))) {
                ans++;
            }
        }
        return ans;
    }
}
func isVowel(c byte) bool {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
}

func vowelStrings(words []string, left int, right int) int {
    ans := 0
    for i := left; i <= right; i++ {
        w := words[i]
        if isVowel(w[0]) && isVowel(w[len(w)-1]) {
            ans++
        }
    }
    return ans
}
class Solution {
public:
    bool isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }

    int vowelStrings(vector<string>& words, int left, int right) {
        int ans = 0;
        for (int i = left; i <= right; ++i) {
            const string& w = words[i];
            if (isVowel(w.front()) && isVowel(w.back())) {
                ++ans;
            }
        }
        return ans;
    }
};
class Solution:
    def vowelStrings(self, words: List[str], left: int, right: int) -> int:
        vowels = {"a", "e", "i", "o", "u"}
        ans = 0
        for i in range(left, right + 1):
            w = words[i]
            if w[0] in vowels and w[-1] in vowels:
                ans += 1
        return ans
const isVowel = (c) => {
  return c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u';
};

var vowelStrings = function(words, left, right) {
  let ans = 0;
  for (let i = left; i <= right; i++) {
    const w = words[i];
    if (isVowel(w[0]) && isVowel(w[w.length - 1])) {
      ans++;
    }
  }
  return ans;
};

Comments