Quick Overview

Given a dictionary of lowercase words and many queries, report for each query whether some dictionary word of the same length differs from it in at most one position. Tests going beyond a pairwise scan so that the lookup scales to large dictionaries and large batches of queries.

Dictionary Lookup Allowing One Mismatched Character in Equal-Length Words

Company: Meta

Role: Applied Scientist

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are building a word lookup that tolerates a single misspelled character. Given a dictionary of words and a list of query words, decide for each query whether the dictionary contains a word of the same length that differs from the query in at most one position. Words of a different length never match, so an inserted or deleted character is not tolerated. ### Function Signature ```python def fuzzy_lookup(words: list[str], queries: list[str]) -> list[bool]: ``` ### Rules - Two strings match when they have the same length and their characters differ at no more than one index. An exact match counts as a match. - Return one boolean per query, in the order of `queries`: `True` if at least one dictionary word matches that query, otherwise `False`. - The dictionary may contain duplicate words; duplicates do not change any answer. ### Constraints - `1 <= len(words) <= 10^5` - `1 <= len(queries) <= 10^5` - Every word and every query has length between 1 and 20 inclusive. - Words and queries contain only lowercase English letters `a` to `z`. ### Examples **Example 1** ```text Input: words = ["hello", "world", "help"] queries = ["hallo", "hello", "helps", "word", "wprld", "hekko"] Output: [True, True, False, False, True, False] ``` `hallo` differs from `hello` only at index 1, and `hello` is an exact match. `helps` differs from `hello` at two indices and from `world` at five, and `help` has a different length. The only four-letter word, `help`, differs from `word` at all four indices. `wprld` differs from `world` only at index 1. `hekko` differs from `hello` at indices 2 and 3. **Example 2** ```text Input: words = ["a"] queries = ["b", "a", "ab"] Output: [True, True, False] ``` `b` differs from `a` at one index, `a` matches exactly, and `ab` has a different length from every dictionary word.

Overview: Given a dictionary of lowercase words and many queries, report for each query whether some dictionary word of the same length differs from it in at most one position. Tests going beyond a pairwise scan so that the lookup scales to large dictionaries and large batches of queries.

Read the full Meta Applied Scientist interview experience this question came from

You are building a word lookup that tolerates a single misspelled character. You are given a dictionary `words` and a list of query words `queries`. For each query, decide whether the dictionary contains a word of the same length that differs from the query in at most one position. Two strings **match** when they have the same length and their characters differ at no more than one index. An exact match counts as a match. Words of a different length never match, so an inserted or deleted character is not tolerated. Return a list with one boolean per query, in the order of `queries`: `True` if at least one dictionary word matches that query, otherwise `False`. The dictionary may contain duplicate words; duplicates do not change any answer. ### Constraints - `1 <= len(words) <= 10^5` - `1 <= len(queries) <= 10^5` - Every word and every query has length between 1 and 20 inclusive. - Words and queries contain only lowercase English letters `a` to `z`. The result holds only booleans, so no value can exceed 2^31-1. ### Example 1 ```text Input: words = ["hello", "world", "help"] queries = ["hallo", "hello", "helps", "word", "wprld", "hekko"] Output: [True, True, False, False, True, False] ``` `hallo` differs from `hello` only at index 1, and `hello` is an exact match. `helps` differs from `hello` at two indices and from `world` at five, and `help` has a different length. The only four-letter word, `help`, differs from `word` at all four indices. `wprld` differs from `world` only at index 1. `hekko` differs from `hello` at indices 2 and 3. ### Example 2 ```text Input: words = ["a"] queries = ["b", "a", "ab"] Output: [True, True, False] ``` `b` differs from `a` at one index, `a` matches exactly, and `ab` has a different length from every dictionary word.

Constraints

  • 1 <= len(words) <= 10^5
  • 1 <= len(queries) <= 10^5
  • Every word and every query has length between 1 and 20 inclusive.
  • Words and queries contain only lowercase English letters a to z.

Examples

Input: (['hello', 'world', 'help'], ['hallo', 'hello', 'helps', 'word', 'wprld', 'hekko'])

Expected Output: [True, True, False, False, True, False]

Explanation: Source example 1: hallo and wprld differ from hello and world at one index, hello matches exactly, helps and hekko differ from hello at two indices, and help differs from word at all four indices.

Input: (['a'], ['b', 'a', 'ab'])

Expected Output: [True, True, False]

Explanation: Source example 2: b differs from a at one index, a matches exactly, and ab has length 2 while the only word has length 1.

Hints

  1. A query can only match dictionary words of exactly its length; an inserted or deleted character never matches.
  2. Comparing every query with every word can take about 10^10 string comparisons at the maximum input. Every string has at most 20 characters, so look for work that grows with the string length rather than with the dictionary size.
  3. An exact match counts as a match, and duplicate dictionary words never change any answer.

Dictionary Lookup Tolerating One Misspelled Character

hardMetaApplied Scientist

You are building a word lookup that tolerates a single misspelled character. You are given a dictionary words and a list of query words queries. For each query, decide whether the dictionary contains a word of the same length that differs from the query in at most one position.

Two strings match when they have the same length and their characters differ at no more than one index. An exact match counts as a match. Words of a different length never match, so an inserted or deleted character is not tolerated.

Return a list with one boolean per query, in the order of queries: True if at least one dictionary word matches that query, otherwise False. The dictionary may contain duplicate words; duplicates do not change any answer.

Constraints

  • 1 <= len(words) <= 10^5
  • 1 <= len(queries) <= 10^5
  • Every word and every query has length between 1 and 20 inclusive.
  • Words and queries contain only lowercase English letters a to z.

The result holds only booleans, so no value can exceed 2^31-1.

Example 1

Input:  words = ["hello", "world", "help"]
        queries = ["hallo", "hello", "helps", "word", "wprld", "hekko"]
Output: [True, True, False, False, True, False]

hallo differs from hello only at index 1, and hello is an exact match. helps differs from hello at two indices and from world at five, and help has a different length. The only four-letter word, help, differs from word at all four indices. wprld differs from world only at index 1. hekko differs from hello at indices 2 and 3.

Example 2

Input:  words = ["a"]
        queries = ["b", "a", "ab"]
Output: [True, True, False]

b differs from a at one index, a matches exactly, and ab has a different length from every dictionary word.

Examples

Example 1
Input
(['hello', 'world', 'help'], ['hallo', 'hello', 'helps', 'word', 'wprld', 'hekko'])
Output
[True, True, False, False, True, False]
Notes
Source example 1: hallo and wprld differ from hello and world at one index, hello matches exactly, helps and hekko differ from hello at two indices, and help differs from word at all four indices.
Example 2
Input
(['a'], ['b', 'a', 'ab'])
Output
[True, True, False]
Notes
Source example 2: b differs from a at one index, a matches exactly, and ab has length 2 while the only word has length 1.

Constraints

  • •1 <= len(words) <= 10^5
  • •1 <= len(queries) <= 10^5
  • •Every word and every query has length between 1 and 20 inclusive.
  • •Words and queries contain only lowercase English letters a to z.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def fuzzy_lookup(words: list[str], queries: list[str]) -> list[bool]:
Sample call
fuzzy_lookup(['hello', 'world', 'help'], ['hallo', 'hello', 'helps', 'word', 'wprld', 'hekko'])
Language
Case 1
Input
(['hello', 'world', 'help'], ['hallo', 'hello', 'helps', 'word', 'wprld', 'hekko'])
Expected
[True, True, False, False, True, False]
Case 2
Input
(['a'], ['b', 'a', 'ab'])
Expected
[True, True, False]

Browse More Questions

Show the approach

Approach

Algorithm: two equal-length strings differ in at most one index exactly when, for some index i, they become identical once the character at index i is hidden. For each index i from 0 up to the longest dictionary word length minus 1, build a set holding every dictionary word longer than i with its character at i replaced by ''. Then, for every query that is longer than i and not yet answered True, replace its character at i by '' and mark it True if that masked string is in the set.

Correctness: masking keeps the length and '*' is never a lowercase letter, so a masked query equals a masked word only when both have the same length and agree at every index other than i, which means they differ at most at index i. That is a valid match. Conversely, an exact match is found at index 0 because every string has at least one character. A match that differs only at index k is found at index k, and k is smaller than the word's length, which is at most the longest word length. So a query ends up True exactly when some dictionary word matches it.

Edge cases: a query whose length no word has, including one longer than every word (an insertion relative to a word) or shorter (a deletion), never meets an equal masked string and stays False. Duplicate words collapse in each set and change nothing. Repeated queries are answered independently, each in its own position. Only one index's set is alive at a time, so memory stays at one masked copy of the dictionary (at most 10^5 strings), not 20 copies.

Time complexity:
O(L^2 * (n + m)), where n = len(words), m = len(queries) and L <= 20 is the maximum string length
Space complexity:
O(n * L + m)