LC.P2586[统计范围内的元音字符串数]

方法一:遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {

private static final Set<Character> set = new HashSet<>(Set.of('a', 'e', 'i', 'o', 'u'));

public int vowelStrings(String[] words, int left, int right) {
int ans = 0;
for (int i = left; i <= right; ++i) {
String s = words[i];
if (set.contains(s.charAt(0)) && set.contains(s.charAt(s.length() - 1))) {
++ans;
}
}
return ans;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(1)$