Médio 0 resolvidos 0% de aceitação

Spell Checker

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

O que você vai aprender

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

Problema

Ver tradução

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.

Restrições

0 ≤ dictionary size ≤ 2,000

0 ≤ length of text ≤ 10,000

Exemplos

Exemplo 1
Entrada:
dictionary = ["the", "cat", "sat", "on", "mat"]
text = "The cat sat on teh mat, the cta!"

Saída esperada:
expected = ["teh", "cta"]
Entrada:
std::vector<std::string> dictionary = {"the", "cat", "sat", "on", "mat"};
std::string text = "The cat sat on teh mat, the cta!";

Saída esperada:
std::vector<std::string> expected = {"teh", "cta"};
Entrada:
string[] dictionary = new string[] {"the", "cat", "sat", "on", "mat"};
string text = "The cat sat on teh mat, the cta!";

Saída esperada:
string[] expected = new string[] {"teh", "cta"};

Explicação

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

Exemplo 2
Entrada:
dictionary = ["Don't", "stop"]
text = "don't STOP, dont stop dont"

Saída esperada:
expected = ["dont"]
Entrada:
std::vector<std::string> dictionary = {"Don't", "stop"};
std::string text = "don't STOP, dont stop dont";

Saída esperada:
std::vector<std::string> expected = {"dont"};
Entrada:
string[] dictionary = new string[] {"Don't", "stop"};
string text = "don't STOP, dont stop dont";

Saída esperada:
string[] expected = new string[] {"dont"};

Explicação

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

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Lowercase the dictionary words once when you build the set.

Dica 2

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

Dica 3

Keep a second set of words already reported.

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.