Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Vowel Counter by Position

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ length of text ≤ 10,000

Ví dụ

Ví dụ 1
Đầu vào:
text = "Hello World"

Kết quả mong đợi:
expected = [
    [],
    [1],
    [],
    [4, 7],
    []
]
Đầu vào:
std::string text = "Hello World";

Kết quả mong đợi:
std::vector<std::vector<long long>> expected = {
    {},
    {1},
    {},
    {4, 7},
    {}
};
Đầu vào:
string text = "Hello World";

Kết quả mong đợi:
long[][] expected = new long[][] {
    new long[] {},
    new long[] {1},
    new long[] {},
    new long[] {4, 7},
    new long[] {}
};

Giải thích

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

Ví dụ 2
Đầu vào:
text = "AU audio"

Kết quả mong đợi:
expected = [
    [0, 3],
    [],
    [6],
    [7],
    [1, 4]
]
Đầu vào:
std::string text = "AU audio";

Kết quả mong đợi:
std::vector<std::vector<long long>> expected = {
    {0, 3},
    {},
    {6},
    {7},
    {1, 4}
};
Đầu vào:
string text = "AU audio";

Kết quả mong đợi:
long[][] expected = new long[][] {
    new long[] {0, 3},
    new long[] {},
    new long[] {6},
    new long[] {7},
    new long[] {1, 4}
};

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Make five empty lists before scanning.

Gợi ý 2

Lowercase each character before you compare it.

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.