Search~10 min2s · 256 MBtarget O(n) time, O(1) spacestrings
A search box in an app shows a small hint under what you type: how many vowels and how many consonants the text has.
People type capitals, digits, spaces and punctuation too. The hint ignores everything that is not a letter, and it treats capital and small letters the same.
Write count_vowels_consonants(query).
query is the text typed so far.a, e, i, o or u, in either case. Every other letter is a consonant, so y is a consonant.[vowels, consonants].Input: query = "Hello, World 42"
Output: [3, 7]The table counts the letters word by word.
| Word | Vowels | Consonants |
|---|---|---|
| Hello, | 2 | 3 |
| World | 1 | 4 |
| 42 | 0 | 0 |
| Total | 3 | 7 |
The letters are H, e, l, l, o, W, o, r, l, d. Three are vowels and seven are consonants. The digits 4 and 2, the comma and the spaces are skipped.
Input: query = "Sky"
Output: [0, 3]The letter y is a consonant here, so Sky has no vowels and three consonants.
Input: query = "123 !?"
Output: [0, 0]There are no letters, so both counts are 0.
1 ≤ query.length ≤ 1,000,000 (characters). , ! ? ' -A million characters need a million steps at most, so one pass is cheap. Looking at each character once, and doing a fixed amount of work for it, is all you need. A loop that counts again from the start after every character costs about n × n / 2 steps, 5 × 10^11 at this size.