Design a Trie
Medium
Design and implement a trie data structure that supports the following operations:
insert(word: str) -> None: Inserts a word into the trie.search(word: str) -> bool: Returns true if a word exists in the trie, and false if not.has_prefix(prefix: str) -> bool: Returns true if the trie contains a word with the given prefix, and false if not.
Example:
Input: [
insert('top'),
insert('bye'),
has_prefix('to'),
search('to'),
insert('to'),
search('to')
]
Output: [True, False, True]
Explanation:
insert("top") # trie has: "top"
insert("bye") # trie has: "top" and "bye"
has_prefix("to") # prefix "to" exists in the string "top": return True
search("to") # trie does not contain the word "to": return False
insert("to") # trie has: "top", "bye", and "to"
search("to") # trie contains the word "to": return True
Constraints:
- The words and prefixes consist only of lowercase English letters.
- The length of each word and prefix is at least one character.
Insert and Search Words with Wildcards
Medium
Design and implement a data structure that supports the following operations:
insert(word: str) -> None: Inserts a word into the data structure.search(word: str) -> bool: Returns true if a word exists in the data structure and false if not. The word may contain wildcards ('.') that can represent any letter.
Example:
Input: [
insert('band'),
insert('rat'),
search('ra.'),
search('b..'),
insert('ran'),
search('.an')
]
Output: [True, False, True]
Explanation:
insert("band") # data structure has: "band"
insert("rat") # data structure has: "band" and "rat"
search("ra.") # "ra." matches "rat": return True
search("b..") # no three-letter word starting with ‘b' in the
# data structure: return False
insert("ran") # data structure has: "band", "rat", and "ran"
search(".an") # ".an" matches "ran": return True
Constraints:
- Words will only contain lowercase English letters and
'.'characters.
Find All Words on a Board
Hard
Given a 2D board of characters and an array of words, find all the words in the array that can be formed by tracing a path through adjacent cells in the board. Adjacent cells are those which horizontally or vertically neighbor each other. We can’t use the same cell more than once for a single word.
Example:

Input: board = [['b', 'y', 's'], ['r', 't', 'e'], ['a', 'i', 'n']],
words = ['byte', 'bytes', 'rat', 'rain', 'trait', 'train']
Output: ['byte', 'bytes', 'rain', 'train']