|
| 1 | +# [2586. Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) |
| 2 | + |
| 3 | +[中文文档](/solution/2500-2599/2586.Count%20the%20Number%20of%20Vowel%20Strings%20in%20Range/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p> |
| 8 | + |
| 9 | +<p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p> |
| 10 | + |
| 11 | +<p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> words = ["are","amy","u"], left = 0, right = 2 |
| 18 | +<strong>Output:</strong> 2 |
| 19 | +<strong>Explanation:</strong> |
| 20 | +- "are" is a vowel string because it starts with 'a' and ends with 'e'. |
| 21 | +- "amy" is not a vowel string because it does not end with a vowel. |
| 22 | +- "u" is a vowel string because it starts with 'u' and ends with 'u'. |
| 23 | +The number of vowel strings in the mentioned range is 2. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p><strong class="example">Example 2:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 |
| 30 | +<strong>Output:</strong> 3 |
| 31 | +<strong>Explanation:</strong> |
| 32 | +- "aeo" is a vowel string because it starts with 'a' and ends with 'o'. |
| 33 | +- "mu" is not a vowel string because it does not start with a vowel. |
| 34 | +- "ooo" is a vowel string because it starts with 'o' and ends with 'o'. |
| 35 | +- "artro" is a vowel string because it starts with 'a' and ends with 'o'. |
| 36 | +The number of vowel strings in the mentioned range is 3. |
| 37 | +</pre> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Constraints:</strong></p> |
| 41 | + |
| 42 | +<ul> |
| 43 | + <li><code>1 <= words.length <= 1000</code></li> |
| 44 | + <li><code>1 <= words[i].length <= 10</code></li> |
| 45 | + <li><code>words[i]</code> consists of only lowercase English letters.</li> |
| 46 | + <li><code>0 <= left <= right < words.length</code></li> |
| 47 | +</ul> |
| 48 | + |
| 49 | +## Solutions |
| 50 | + |
| 51 | +<!-- tabs:start --> |
| 52 | + |
| 53 | +### **Python3** |
| 54 | + |
| 55 | +```python |
| 56 | +class Solution: |
| 57 | + def vowelStrings(self, words: List[str], left: int, right: int) -> int: |
| 58 | + return sum(w[0] in 'aeiou' and w[-1] in 'aeiou' for w in words[left: right + 1]) |
| 59 | +``` |
| 60 | + |
| 61 | +### **Java** |
| 62 | + |
| 63 | +```java |
| 64 | +class Solution { |
| 65 | + public int vowelStrings(String[] words, int left, int right) { |
| 66 | + int ans = 0; |
| 67 | + for (int i = left; i <= right; ++i) { |
| 68 | + var w = words[i]; |
| 69 | + if (check(w.charAt(0)) && check(w.charAt(w.length() - 1))) { |
| 70 | + ++ans; |
| 71 | + } |
| 72 | + } |
| 73 | + return ans; |
| 74 | + } |
| 75 | + |
| 76 | + private boolean check(char c) { |
| 77 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### **C++** |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + int vowelStrings(vector<string>& words, int left, int right) { |
| 88 | + auto check = [](char c) -> bool { |
| 89 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; |
| 90 | + }; |
| 91 | + int ans = 0; |
| 92 | + for (int i = left; i <= right; ++i) { |
| 93 | + auto w = words[i]; |
| 94 | + ans += check(w[0]) && check(w[w.size() - 1]); |
| 95 | + } |
| 96 | + return ans; |
| 97 | + } |
| 98 | +}; |
| 99 | +``` |
| 100 | +
|
| 101 | +### **Go** |
| 102 | +
|
| 103 | +```go |
| 104 | +func vowelStrings(words []string, left int, right int) (ans int) { |
| 105 | + check := func(c byte) bool { |
| 106 | + return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' |
| 107 | + } |
| 108 | + for _, w := range words[left : right+1] { |
| 109 | + if check(w[0]) && check(w[len(w)-1]) { |
| 110 | + ans++ |
| 111 | + } |
| 112 | + } |
| 113 | + return |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### **...** |
| 118 | + |
| 119 | +``` |
| 120 | +
|
| 121 | +``` |
| 122 | + |
| 123 | +<!-- tabs:end --> |
0 commit comments