All challenges
Easy 0 solved 0% acceptance

Vowel Counter by Position

List the positions of every a, e, i, o and u in a piece of text.

What you'll learn

  • Loop over a string with indexes
  • Compare characters ignoring case
  • Group results into lists

Problem

A reading tutor app highlights vowels. For each of the five vowels a, e, i, o and u, find every index where it appears in the text. Uppercase and lowercase count as the same vowel, and y is not a vowel here. Indexes start at 0 and count every character, including spaces and punctuation.

Implement vowel_positions(text).

Input

text: The text to scan.

Output

Return five lists in the order a, e, i, o, u. Each list holds that vowel's indexes in increasing order and is empty if the vowel does not appear.

Constraints

0 ≤ length of text ≤ 10,000

Examples

Example 1
Input:
text = "Hello World"

Expected output:
expected = [
    [],
    [1],
    [],
    [4, 7],
    []
]
Input:
std::string text = "Hello World";

Expected output:
std::vector<std::vector<long long>> expected = {
    {},
    {1},
    {},
    {4, 7},
    {}
};
Input:
string text = "Hello World";

Expected output:
long[][] expected = new long[][] {
    new long[] {},
    new long[] {1},
    new long[] {},
    new long[] {4, 7},
    new long[] {}
};

Explanation

e is at index 1 and o at 4 and 7; there is no a, i or u: [[], [1], [], [4, 7], []].

Example 2
Input:
text = "AU audio"

Expected output:
expected = [
    [0, 3],
    [],
    [6],
    [7],
    [1, 4]
]
Input:
std::string text = "AU audio";

Expected output:
std::vector<std::vector<long long>> expected = {
    {0, 3},
    {},
    {6},
    {7},
    {1, 4}
};
Input:
string text = "AU audio";

Expected output:
long[][] expected = new long[][] {
    new long[] {0, 3},
    new long[] {},
    new long[] {6},
    new long[] {7},
    new long[] {1, 4}
};

Explanation

Uppercase counts too: a at 0 and 3, u at 1 and 4, i at 6, o at 7: [[0, 3], [], [6], [7], [1, 4]].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Make five empty lists before scanning.

Hint 2

Lowercase each character before you compare it.

Hint 3

One pass over the text is enough: drop each index into the right list.

Expected complexity

O(n) time, O(n) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.