Tous les défis
Facile 0 résolus 0 % de réussite

Vowel Counter by Position

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

0 ≤ length of text ≤ 10,000

Exemples

Exemple 1
Entrée :
text = "Hello World"

Sortie attendue :
expected = [
    [],
    [1],
    [],
    [4, 7],
    []
]
Entrée :
std::string text = "Hello World";

Sortie attendue :
std::vector<std::vector<long long>> expected = {
    {},
    {1},
    {},
    {4, 7},
    {}
};
Entrée :
string text = "Hello World";

Sortie attendue :
long[][] expected = new long[][] {
    new long[] {},
    new long[] {1},
    new long[] {},
    new long[] {4, 7},
    new long[] {}
};

Explication

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

Exemple 2
Entrée :
text = "AU audio"

Sortie attendue :
expected = [
    [0, 3],
    [],
    [6],
    [7],
    [1, 4]
]
Entrée :
std::string text = "AU audio";

Sortie attendue :
std::vector<std::vector<long long>> expected = {
    {0, 3},
    {},
    {6},
    {7},
    {1, 4}
};
Entrée :
string text = "AU audio";

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

Explication

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]].

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Make five empty lists before scanning.

Indice 2

Lowercase each character before you compare it.

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.