Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Vowel Counter by Position

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

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

0 ≤ length of text ≤ 10,000

Beispiele

Beispiel 1
Eingabe:
text = "Hello World"

Erwartete Ausgabe:
expected = [
    [],
    [1],
    [],
    [4, 7],
    []
]
Eingabe:
std::string text = "Hello World";

Erwartete Ausgabe:
std::vector<std::vector<long long>> expected = {
    {},
    {1},
    {},
    {4, 7},
    {}
};
Eingabe:
string text = "Hello World";

Erwartete Ausgabe:
long[][] expected = new long[][] {
    new long[] {},
    new long[] {1},
    new long[] {},
    new long[] {4, 7},
    new long[] {}
};

Erläuterung

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

Beispiel 2
Eingabe:
text = "AU audio"

Erwartete Ausgabe:
expected = [
    [0, 3],
    [],
    [6],
    [7],
    [1, 4]
]
Eingabe:
std::string text = "AU audio";

Erwartete Ausgabe:
std::vector<std::vector<long long>> expected = {
    {0, 3},
    {},
    {6},
    {7},
    {1, 4}
};
Eingabe:
string text = "AU audio";

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

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Make five empty lists before scanning.

Hinweis 2

Lowercase each character before you compare it.

Hinweis 3

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

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.