Medium 0 solved 0% acceptance

Spell Checker

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

What you'll learn

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

Problem

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.

Constraints

0 ≤ dictionary size ≤ 2,000

0 ≤ length of text ≤ 10,000

Examples

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

Expected output:
expected = ["teh", "cta"]
Input:
std::vector<std::string> dictionary = {"the", "cat", "sat", "on", "mat"};
std::string text = "The cat sat on teh mat, the cta!";

Expected output:
std::vector<std::string> expected = {"teh", "cta"};
Input:
string[] dictionary = new string[] {"the", "cat", "sat", "on", "mat"};
string text = "The cat sat on teh mat, the cta!";

Expected output:
string[] expected = new string[] {"teh", "cta"};

Explanation

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

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

Expected output:
expected = ["dont"]
Input:
std::vector<std::string> dictionary = {"Don't", "stop"};
std::string text = "don't STOP, dont stop dont";

Expected output:
std::vector<std::string> expected = {"dont"};
Input:
string[] dictionary = new string[] {"Don't", "stop"};
string text = "don't STOP, dont stop dont";

Expected output:
string[] expected = new string[] {"dont"};

Explanation

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

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Lowercase the dictionary words once when you build the set.

Hint 2

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

Hint 3

Keep a second set of words already reported.

Expected complexity

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

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

The best passing solution of each coder. Faster, smaller and better-reviewed solutions rank higher.

No passing solutions yet. Solve it to take first place.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.