KatInterview

Typo-Tolerant Word Lookup with One Edit

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designhash-mapedit-distancestrings

Last updated

Answer batches of lookups against a fixed word list, returning every word within Levenshtein distance 1 of the query, using a deletion-based hash index.

Typo-Tolerant Word Lookup

A spell-suggestion feature works against a fixed list of words. A word is a suggestion for a query if the two strings are at most one edit apart. Because many queries hit the same list, each lookup should be much faster than comparing the query to every word.

The service is driven by a sequence of operations, which you process in one function call.

Edit distance

Use the standard Levenshtein distance. One edit is one of:

  • inserting a single character,
  • deleting a single character,
  • replacing a single character with another one.

Swapping two adjacent characters is not a single edit. For example, "appel" and "apple" are at distance 2.

A word matches a query if its distance to the query is 0 or 1.

Task

Implement a function that receives:

  • words: the list of dictionary words. It may contain duplicates, which should be treated as a single word.
  • operations: a list of operation names.
  • arguments: a list of the same length as operations. arguments[i] is the list of arguments for operations[i].

Process the operations in order. The dictionary is fixed for the whole call. Each operation contributes one entry to the returned list:

  • "init": nothing needs to be done. Output None for it.
  • "search": arguments[i][0] is the query string. Output a list of all distinct dictionary words whose edit distance to the query is 0 or 1, sorted in ascending lexicographic order. Output [] if there are none.

Any other operation name should raise a ValueError.

Constraints

  • The number of distinct words is at most 50,000.
  • Each word has length at most 20.
  • Queries may be empty, and may be much longer or shorter than every dictionary word.
  • The dictionary may be empty.
  • Words and queries are strings. Comparison is case-sensitive and exact.
  • Lookups should be considerably faster than a scan over the whole dictionary.

Examples

Example 1

Input: fuzzy_lookup_batch(['apple', 'apply', 'banana', 'pear'], ['search', 'search', 'search'], [['appel'], ['apple'], ['banan']])

Output: [[], ['apple', 'apply'], ['banana']]

Hints

Hint 1

Two strings within one edit share a common string if you allow deleting at most one character from each of them. Which edit types does this cover: insertion, deletion, substitution?

Hint 2

Precompute a hash map from every 'word with at most one character removed' (including the word itself) to the dictionary words that produce it. A query then only needs about L+1 map lookups.

Hint 3

Sharing a key is necessary but not sufficient: 'ab' and 'ba' both reduce to 'a' but are at distance 2. Verify each candidate with a linear scan that tolerates at most one mismatch, and deduplicate and sort the results.