Moyen 0 résolus 0 % de réussite

Spell Checker

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

0 ≤ dictionary size ≤ 2,000

0 ≤ length of text ≤ 10,000

Exemples

Exemple 1
Entrée :
dictionary = ["the", "cat", "sat", "on", "mat"]
text = "The cat sat on teh mat, the cta!"

Sortie attendue :
expected = ["teh", "cta"]
Entrée :
std::vector<std::string> dictionary = {"the", "cat", "sat", "on", "mat"};
std::string text = "The cat sat on teh mat, the cta!";

Sortie attendue :
std::vector<std::string> expected = {"teh", "cta"};
Entrée :
string[] dictionary = new string[] {"the", "cat", "sat", "on", "mat"};
string text = "The cat sat on teh mat, the cta!";

Sortie attendue :
string[] expected = new string[] {"teh", "cta"};

Explication

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

Exemple 2
Entrée :
dictionary = ["Don't", "stop"]
text = "don't STOP, dont stop dont"

Sortie attendue :
expected = ["dont"]
Entrée :
std::vector<std::string> dictionary = {"Don't", "stop"};
std::string text = "don't STOP, dont stop dont";

Sortie attendue :
std::vector<std::string> expected = {"dont"};
Entrée :
string[] dictionary = new string[] {"Don't", "stop"};
string text = "don't STOP, dont stop dont";

Sortie attendue :
string[] expected = new string[] {"dont"};

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Lowercase the dictionary words once when you build the set.

Indice 2

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

Indice 3

Keep a second set of words already reported.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.