Add and Search Word
Implement wordDictionarySearch
Given a list of
words to insert and a single pattern to search for, decide whether any inserted word matches it — where the pattern may contain '.', a wildcard that matches any single letter in that position. A pattern with no dots at all is just an ordinary exact-word lookup.
An ordinary trie walk follows exactly one child per character. A wildcard breaks that: at a '.', there's no single "next node" to commit to, so the search has to try every existing child and recurse into each — backtracking out of any branch that doesn't eventually pan out, and returning true the instant one does. Positions without a dot are still cheap, single-child steps; it's only the wildcards that fan the search out.
Example 1:
Input: words = ["bad","dad","mad"], pattern = "pad"
Output: false
Example 2:
Input: words = ["bad","dad","mad"], pattern = "bad"
Output: true
Example 3:
Input: words = ["bad","dad","mad"], pattern = ".ad"
Output: true
+ 11 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ words.length ≤ 1000 - ●
1 ≤ words[i].length ≤ 25 - ●
0 ≤ pattern.length ≤ 25 - ●
words[i] consists of lowercase English letters only - ●
pattern consists of lowercase English letters and the wildcard '.'
words =
["bad", "dad", "mad"]
pattern =
pad