ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Contains the Character

Keep only the words that contain a given letter, ignoring case.

สิ่งที่คุณจะได้เรียนรู้

  • Check whether a string contains a character
  • Compare letters while ignoring case
  • Keep matching items in their original order

โจทย์

ดูฉบับแปล

For a word game, players enter a list of words and a letter, and the game shows only the words that use that letter. Upper and lower case count as the same letter, so K matches kite and k matches Kansas.

Implement contains_character(words, letter).

Input

words: The list of words.

letter: A single English letter.

Output

Return the words that contain the letter, in their original order and spelling. Repeated words are kept as many times as they appear. If no word matches, return an empty list.

เงื่อนไข

0 ≤ length of words ≤ 1000

Each word has 1 to 30 ASCII letters

letter is one ASCII letter

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
words = ["apple", "Berry", "kiwi", "Grape"]
letter = "r"

ผลลัพธ์ที่คาดหวัง:
expected = ["Berry", "Grape"]
อินพุต:
std::vector<std::string> words = {"apple", "Berry", "kiwi", "Grape"};
std::string letter = "r";

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Berry", "Grape"};
อินพุต:
string[] words = new string[] {"apple", "Berry", "kiwi", "Grape"};
string letter = "r";

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Berry", "Grape"};

คำอธิบาย

Berry and Grape contain an r; apple and kiwi do not.

ตัวอย่างที่ 2
อินพุต:
words = ["Ohio", "Utah", "Iowa"]
letter = "O"

ผลลัพธ์ที่คาดหวัง:
expected = ["Ohio", "Iowa"]
อินพุต:
std::vector<std::string> words = {"Ohio", "Utah", "Iowa"};
std::string letter = "O";

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"Ohio", "Iowa"};
อินพุต:
string[] words = new string[] {"Ohio", "Utah", "Iowa"};
string letter = "O";

ผลลัพธ์ที่คาดหวัง:
string[] expected = new string[] {"Ohio", "Iowa"};

คำอธิบาย

Case is ignored, so Ohio and Iowa both match.

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Convert both the word and the letter to the same case before comparing.

คำใบ้ 2

Return the original word, not the converted copy.

คำใบ้ 3

Do not remove duplicates; just test each word on its own.

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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

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

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