Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Spell Checker

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

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

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

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ dictionary size ≤ 2,000

0 ≤ length of text ≤ 10,000

Ví dụ

Ví dụ 1
Đầu vào:
dictionary = ["the", "cat", "sat", "on", "mat"]
text = "The cat sat on teh mat, the cta!"

Kết quả mong đợi:
expected = ["teh", "cta"]
Đầu vào:
std::vector<std::string> dictionary = {"the", "cat", "sat", "on", "mat"};
std::string text = "The cat sat on teh mat, the cta!";

Kết quả mong đợi:
std::vector<std::string> expected = {"teh", "cta"};
Đầu vào:
string[] dictionary = new string[] {"the", "cat", "sat", "on", "mat"};
string text = "The cat sat on teh mat, the cta!";

Kết quả mong đợi:
string[] expected = new string[] {"teh", "cta"};

Giải thích

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

Ví dụ 2
Đầu vào:
dictionary = ["Don't", "stop"]
text = "don't STOP, dont stop dont"

Kết quả mong đợi:
expected = ["dont"]
Đầu vào:
std::vector<std::string> dictionary = {"Don't", "stop"};
std::string text = "don't STOP, dont stop dont";

Kết quả mong đợi:
std::vector<std::string> expected = {"dont"};
Đầu vào:
string[] dictionary = new string[] {"Don't", "stop"};
string text = "don't STOP, dont stop dont";

Kết quả mong đợi:
string[] expected = new string[] {"dont"};

Giải thích

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

Hướng tiếp cận

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

Gợi ý 1

Lowercase the dictionary words once when you build the set.

Gợi ý 2

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

Gợi ý 3

Keep a second set of words already reported.

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

O(D + T) time, O(D + T) 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.