Automatic Typo Correction
InterviewTime limit1sMemory limit128 MB
Given a dictionary, classify each queried word as correct, a misspelling of the first similar dictionary word, or unknown, using three specific edit types.
- Level
Medium5 of 10
- Topics
- String, Hash map, Implementation, Brute force
- Solved
- No attempts yet
Problem
We want to build a spell-checker that fixes typos automatically.
The checker can fix only the following three kinds of typos:
- One letter is missing (e.g.
lettertyped asleter) or one extra letter is added (e.g.lettertyped aslettter). - One letter is wrong (e.g.
lettertyped asketter). - Two adjacent letters are swapped (e.g.
lettertyped aslettre).
The checker holds a dictionary of words and uses it to correct typos. If the word the user typed is in the dictionary, it is correct. Otherwise it is replaced with the most similar word in the dictionary.
Two words and are called similar if word can be turned into dictionary word by applying exactly one of the three methods above exactly once. If no similar word exists in the dictionary, the word is treated as unknown and is not corrected.
Input
The first line contains the number of words in the dictionary, ().
Each of the next lines contains one dictionary word.
The next line contains the number of words to check, ().
Each of the next lines contains one word to check.
Every word consists of lowercase letters only and has length between and .
Output
For each word to check, print one line: the given word followed by one of the following.
w is correct: the word is in the dictionary.w is a misspelling of x: the word is not in the dictionary andxis a dictionary word similar to it. If several similar words exist, print the one that appears earliest in the input.w is unknown: neither of the above holds.
Here w is the word to check that was given in the input.