中級 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。