簡単 0件解決済み 正答率0%

Vowel Counter by Position

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

0 ≤ length of text ≤ 10,000

例

例 1
入力:
text = "Hello World"

期待される出力:
expected = [
    [],
    [1],
    [],
    [4, 7],
    []
]
入力:
std::string text = "Hello World";

期待される出力:
std::vector<std::vector<long long>> expected = {
    {},
    {1},
    {},
    {4, 7},
    {}
};
入力:
string text = "Hello World";

期待される出力:
long[][] expected = new long[][] {
    new long[] {},
    new long[] {1},
    new long[] {},
    new long[] {4, 7},
    new long[] {}
};

解説

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

例 2
入力:
text = "AU audio"

期待される出力:
expected = [
    [0, 3],
    [],
    [6],
    [7],
    [1, 4]
]
入力:
std::string text = "AU audio";

期待される出力:
std::vector<std::vector<long long>> expected = {
    {0, 3},
    {},
    {6},
    {7},
    {1, 4}
};
入力:
string text = "AU audio";

期待される出力:
long[][] expected = new long[][] {
    new long[] {0, 3},
    new long[] {},
    new long[] {6},
    new long[] {7},
    new long[] {1, 4}
};

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Make five empty lists before scanning.

ヒント 2

Lowercase each character before you compare it.

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。