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

Spell Checker

List the words in a text that are missing from a dictionary, ignoring case and punctuation.

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

  • Load words into a set for fast lookups
  • Split text into words with a character rule
  • Report results in first-seen order without duplicates

โจทย์

ดูฉบับแปล

A word processor underlines words it does not know. Load the dictionary into a set (a hash table) so each lookup is fast, then scan the text.

A word is a maximal run of English letters (A-Z, a-z) and apostrophes; every other character (spaces, digits, punctuation) separates words. Words are compared without regard to case, so The matches the dictionary entry the.

Implement spell_checker(dictionary, text).

Input

dictionary: The known words. They may be in any case.

text: The text to check.

Output

Return the misspelled words in lowercase, in the order they first appear in the text, each listed only once. Return an empty list when every word is known.

เงื่อนไข

0 ≤ dictionary size ≤ 2,000

0 ≤ length of text ≤ 10,000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
dictionary = ["the", "cat", "sat", "on", "mat"]
text = "The cat sat on teh mat, the cta!"

ผลลัพธ์ที่คาดหวัง:
expected = ["teh", "cta"]
อินพุต:
std::vector<std::string> dictionary = {"the", "cat", "sat", "on", "mat"};
std::string text = "The cat sat on teh mat, the cta!";

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"teh", "cta"};
อินพุต:
string[] dictionary = new string[] {"the", "cat", "sat", "on", "mat"};
string text = "The cat sat on teh mat, the cta!";

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

คำอธิบาย

teh and cta are not in the dictionary. Case and the comma do not matter for the other words: ["teh", "cta"].

ตัวอย่างที่ 2
อินพุต:
dictionary = ["Don't", "stop"]
text = "don't STOP, dont stop dont"

ผลลัพธ์ที่คาดหวัง:
expected = ["dont"]
อินพุต:
std::vector<std::string> dictionary = {"Don't", "stop"};
std::string text = "don't STOP, dont stop dont";

ผลลัพธ์ที่คาดหวัง:
std::vector<std::string> expected = {"dont"};
อินพุต:
string[] dictionary = new string[] {"Don't", "stop"};
string text = "don't STOP, dont stop dont";

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

คำอธิบาย

don't keeps its apostrophe and matches; dont is unknown and is listed once: ["dont"].

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

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

คำใบ้ 1

Lowercase the dictionary words once when you build the set.

คำใบ้ 2

Build the current word character by character and finish it when you meet a non-letter that is not an apostrophe.

คำใบ้ 3

Keep a second set of words already reported.

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

O(D + T) time, O(D + T) extra space

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

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

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

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

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

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

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