898. Dotted Word Finder
A crossword helper stores a growing word bank and answers pattern queries. Build the class DottedWords with an empty constructor.
addWord(word)adds a lowercasewordto the bank (adding a duplicate changes nothing).search(word)receives a pattern made of lowercase letters and dots. A dot stands for exactly one arbitrary letter. Returntrueif some stored word has the same length as the pattern and agrees with it on every non-dot position, otherwisefalse.
Patterns are never empty, and a pattern consisting only of dots simply asks whether a word of that length exists. Store the words in a tree so that a query does not compare against every word.
Example 1
- Input:
- operations = ["DottedWords","addWord","addWord","search","search","search","search"]arguments = [[],["tundra"],["tulip"],["tu...a"],["t.l.p"],["tun"],["t.l.q"]]
- Output:
- [null,null,null,true,true,false,false]
- Explanation:
tu...amatchestundraandt.l.pmatchestulip;tunis shorter than every stored word.
Example 2
- Input:
- operations = ["DottedWords","addWord","search","search","search","search"]arguments = [[],["rye"],["..."],["...."],["r.e"],["r.d"]]
- Output:
- [null,null,true,false,true,false]
- Explanation:
A pattern must have exactly the length of the stored word, and each dot stands for one arbitrary letter.
Constraints
1 ≤ word.length ≤ 14
Words passed to addWord contain only lowercase letters; search patterns contain lowercase letters and '.'.
At most 2000 calls in total.
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Input
- Each case is an operation log.
operationsnames the class first and then each method call;argumentsholds the arguments for each, in the same order. Your answer is one list with a result per operation -nullfor the constructor and for methods that return nothing.
Expected complexity
- Time
- O(26^d * L) per search (d = dots), O(L) per addWord
- Space
- O(total inserted characters)