ง่าย 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก