Odd-Length Strings Containing a Vowel

Coding · Easy · Free problem

Given an array of strings arr, count how many strings s have ODD length AND contain at least one vowel (a, e, i, o, u, case-insensitive).

Complete: ``` count(arr: List[str]) -> int ``` Example: count(['abc', 'xy', 'Hi', 'rhythm']) returns 1 -- only 'abc' (length 3, contains 'a') satisfies both conditions; 'Hi' (len 2), 'rhythm' (len 6) and 'xy' (len 2) have even length. The empty string has length 0 (even) so never counts.

Hints

  1. Two independent predicates per string: odd length, and contains a vowel. AND them.
  2. Use a set of both-case vowels (or lowercase each char) and any(...) so the scan short-circuits on the first vowel.

Worked Solution

Both conditions are independent per-string checks, so a single pass suffices: for each string test len(s) % 2 == 1 and whether any character is a vowel.

```python def count(arr): V = set('aeiouAEIOU') return sum(1 for s in arr if len(s) % 2 == 1 and any(c in V for c in s)) ```

Complexity $O(\sum |s|)$ time, $O(1)$ extra space. The vowel set covers both cases so no lowercasing is needed; any short-circuits on the first vowel found.

Intuition

A warm-up filter-and-count: the answer is just how many array elements pass two cheap boolean tests, so one linear sweep with a short-circuiting vowel check does it.

Open the full interactive solver →