Practice/Microsoft/Leetcode 211. Design Add and Search Words Data Structure
CodingOptional
Design a word dictionary data structure that supports two operations:
The wildcard '.' can appear multiple times in a search pattern and at any position. For example, if "bad" is in the dictionary, then searching for ".ad", "b.d", and "ba." should all return true.
WordDictionary class with an __init__ method (constructor)addWord(word) that adds a word to the dictionary (returns None/void)search(word) that returns true if the word or any matching pattern exists in the dictionaryExample 1:
` Operations: ["WordDictionary", "addWord", "addWord", "addWord", "search", "search", "search", "search"] Arguments: [[], ["bad"], ["dad"], ["mad"], ["pad"], ["bad"], [".ad"], ["b.."]] Output: [null, null, null, null, false, true, true, true]
Explanation: WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False (not in dictionary) wordDictionary.search("bad"); // return True (exact match) wordDictionary.search(".ad"); // return True (matches "bad", "dad", "mad") wordDictionary.search("b.."); // return True (matches "bad") `
Example 2:
` Operations: ["WordDictionary", "addWord", "search", "search", "search"] Arguments: [[], ["test"], ["...."], ["..."], ["t..."]] Output: [null, null, true, false, true]
Explanation: wordDictionary.addWord("test"); wordDictionary.search("...."); // return True (4 wildcards match "test") wordDictionary.search("..."); // return False (3 chars don't match 4-char word) wordDictionary.search("t..."); // return True (matches "test") `