Implement Magic Dictionary Problem

Implement Magic Dictionary Problem — ExecCode Medium DSA Practice

Solve the Implement Magic Dictionary problem on ExecCode. Free online medium DSA practice in Trie. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description Design a data structure that is initialized with a list of different words. Provided a string, you should determine if you can change exactly one character in this string to match any word in the data structure. Implement the MagicDictionary class: MagicDictionary() Initializes the object. void buildDict(String[] dictionary) Sets the data structure with an array of distinct strings dictionary. bool search(String searchWord) Returns true if you can change exactly one character in searchWord to match any string in the data structure, otherwise returns false. Constraints 1 <= dictionary.length <= 100 1 <= dictionary[i].length <= 100 dictionary[i] consists of only lower-case English letters. All the strings in dictionary are distinct. 1 <= searchWord.length <= 100 searchWord consists of only lower-case English letters. buildDict will be called only once before search. At most 100 calls will be made to search.

Examples

Input {"data": "MagicDictionary\nbuild:[\"hello\", \"leetcode\"]\nsearch:\"hello\"\nsearch:\"hhllo\"\nsearch:\"hell\"\nsearch:\"leetcoded\""}; Output false true false false. Input {"data": "MagicDictionary\nbuild:[\"apple\", \"banana\", \"orange\"]\nsearch:\"apply\"\nsearch:\"banaana\"\nsearch:\"orange\"\nsearch:\"grape\""}; Output true false false false. Input {"data": "buildDict([\"hello\",\"leetcode\"]); search(\"hhllo\")"}; Output true

Constraints

1 <= dictionary.length <= 100 1 <= dictionary[i].length <= 100 dictionary[i] consists of only lower-case English letters. All the strings in dictionary are distinct. 1 <= searchWord.length <= 100 searchWord consists of only lower-case English letters. buildDict will be called only once before search. At most 100 calls will be made to search.

Practice Implement Magic Dictionary free on ExecCode. Browse DSA problems, topic map, and placement guides.