Instructions
Write a class WordDictionary that stores lowercase words and checks whether a pattern matches any of them.
The class has these methods:
WordDictionary()creates an empty dictionary.addWord(word)addsword. It returns nothing. Adding a word that is already there changes nothing.search(pattern)returnstrueif at least one added word matchespattern, andfalseif not.
A word matches a pattern when both have the same length and, at every position, the pattern has the same letter as the word or a dot .. A dot matches exactly one letter, any letter. So "b.t" matches "bat" and "bit", but not "bt" or "bats".
How the input is shown
The test gives two lists of the same length. operations[i] is the name of a call and arguments[i] is the list of values for that call. The first call is always "WordDictionary" (create the object). For example, operations = ["WordDictionary", "addWord", "search"] with arguments = [[], ["dog"], ["d.g"]] means: create the dictionary, call addWord("dog"), then call search("d.g").
The expected output is a list with one entry per call: the value the call returns, or null for the constructor and for addWord, which return nothing.
Hint: store the words in a trie (prefix tree). For a letter, follow that one child. For a dot, try every child, and return true as soon as one of them leads to a full match.
Examples
Example 1
- Input:
- ["WordDictionary","addWord","addWord","search","search","search","search"], [[],["pin"],["pan"],["p.n"],["p."],[".an"],["pun"]]
- Output:
- [null,null,null,true,false,true,false]
"p.n" matches both "pin" and "pan". "p." has only 2 letters, and no added word has 2 letters. ".an" matches "pan". "pun" has no dot and was never added.
Example 2
- Input:
- ["WordDictionary","search","addWord","search","search","search"], [[],["..."],["mole"],["..."],["...."],["mol"]]
- Output:
- [null,false,null,false,true,false]
The dictionary is empty at first, so "..." is false. After adding "mole", "...." matches it (any 4-letter word), but "..." does not, because "mole" has 4 letters. "mol" is only the start of "mole", which is not a match.
Example 3
- Input:
- ["WordDictionary","addWord","addWord","addWord","search","search","search"], [[],["oat"],["ear"],["eat"],["e.t"],[".ar"],["..r"]]
- Output:
- [null,null,null,null,true,true,true]
For "e.t" the dot can be "a", giving "eat". For ".ar" the first dot could be "o" (from "oat"), but "oa" is not followed by "r"; the dot must be "e", giving "ear". "..r" also matches "ear". A dot must try every letter, not only the first one that fits.
Constraints
1 <= word.length, pattern.length <= 6wordcontains only lowercase English letters.patterncontains only lowercase English letters and..- At most
30calls toaddWordandsearchin total, after the constructor.
3 examples run when you press Run. Submit checks every hidden test too.
- Input:
- ["WordDictionary","addWord","addWord","search","search","search","search"], [[],["pin"],["pan"],["p.n"],["p."],[".an"],["pun"]]
- Expected:
- [null,null,null,true,false,true,false]
- Input:
- ["WordDictionary","search","addWord","search","search","search"], [[],["..."],["mole"],["..."],["...."],["mol"]]
- Expected:
- [null,false,null,false,true,false]
- Input:
- ["WordDictionary","addWord","addWord","addWord","search","search","search"], [[],["oat"],["ear"],["eat"],["e.t"],[".ar"],["..r"]]
- Expected:
- [null,null,null,null,true,true,true]
Run checks your code against the 3 examples. Submit checks it against all 73 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.