FastPrepMagic Dictionary Queries

Magic Dictionary Queries

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You are given an array dictionary of distinct lowercase words and an array queries.

For each query, return true exactly when you can replace one character with a different lowercase letter so that the resulting word equals a word in dictionary. You may not insert, delete, or reorder characters.

An unchanged dictionary match does not satisfy the rule by itself. However, it still returns true when a different dictionary word of the same length differs from it in exactly one position.

Return the answers in query order.

Function

magicDictionaryQueries(dictionary: String[], queries: String[]) → boolean[]

Examples

Example 1

dictionary = ["hello","leetcode"]queries = ["hello","hhllo","hell","leetcoded"]return = [false,true,false,false]

hhllo becomes hello after one replacement. The exact word hello needs zero replacements, and changing length is forbidden.

Example 2

dictionary = ["hello","hallo"]queries = ["hello","hullo","hallo"]return = [true,true,true]

Each query has a dictionary neighbor at Hamming distance exactly one. For example, exact match hello can change its second character to reach hallo.

Example 3

dictionary = ["a","ab","xyz"]queries = ["b","a","ac","xyy","xy"]return = [true,false,true,true,false]

b, ac, and xyy each differ from a same-length dictionary word in exactly one position. No qualifying alternative exists for a, and xy has no same-length neighbor.

Constraints

  • 1 <= dictionary.length, queries.length <= 100000.
  • 1 <= dictionary[i].length, queries[i].length <= 100.
  • All words contain only lowercase English letters.
  • Words in dictionary are distinct.
  • The total number of characters across dictionary and queries is at most 200000.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean[] magicDictionaryQueries(String[] dictionary, String[] queries) {
    // Write your code here.
}
dictionary["hello","leetcode"]
queries["hello","hhllo","hell","leetcoded"]
expected[false,true,false,false]
Checking account…