Blind 75 · Tries

Design Add and Search Words Data Structure

Medium

Watch the walkthrough (6:31)

Problem

Build a word dictionary that stores words and searches them.
addWord adds a word to the dictionary.
search returns true if any added word matches the given word, and false if not.
In a search, a dot matches any one letter.

Examples

Example 1:
Input: calls = ["WordDictionary","addWord","addWord","addWord","search","search","search","search"], args = [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output: [null,null,null,null,false,true,true,true]
Explanation: each test makes a WordDictionary and then the calls, in order, each with its arguments: addWord("bad"), addWord("dad"), addWord("mad"), search("pad"), search("bad"), search(".ad"), search("b.."). Output has an answer for every call, null where a call returns nothing: pad matches no word; .ad matches bad, dad and mad; b.. matches bad.

Example 2:
Input: calls = ["WordDictionary","addWord","addWord","addWord","search","search","search"], args = [[],["bat"],["cad"],["cat"],[".ad"],[".a"],["ca."]]
Output: [null,null,null,null,true,false,true]
Explanation: .ad matches cad, not bat; .a matches no word, since no added word has two letters; ca. matches cad and cat.

Constraints

1 <= word.length <= 25
word in addWord consists of lowercase English letters.
word in search consists of '.' or lowercase English letters.
A search word has at most 2 dots.
At most 10^4 calls in total are made to addWord and search.

Prerequisites

Tries — a tree keyed by character, where shared prefixes are shared nodes.

How to think about it

1. Trie Walk That Branches on a Dot Optimal

Intuition

Store the words in a trie, as Implement Trie does: each node holds its children by letter and a mark saying a word ends there. A search with no dot walks down it, one child a letter. A dot can be any letter, so it cannot choose a child: it tries every child, and the search becomes a depth-first search that backs up when a path fails and stops at the first that matches. At the word's end, the node must be marked: a path is not a word unless one ends there.

Algorithm

1. A TrieNode holds children, a map from a letter to the next node, and is_end (Java: isEnd), false until a word ends there. The dictionary holds a root node.
2. addWord: from the root, for each letter, make the child if it is missing and step down to it; mark the last node.
3. search: dfs(root, word, 0).
4. dfs(node, word, i): at the end of the word, answer the node's mark. A dot: for each child, if dfs(child, word, i + 1) is true, answer true; when none is, answer false. A letter with no child: false. Otherwise dfs(that child, word, i + 1).

Time & Space

Time O(L) for addWord, L the length of its word, and O(L) for a search with no dot. Each dot can try every child at its depth, up to 26, so a search with d dots is O(26^d x L) at most; LeetCode allows two. Space O(N) for the nodes, N the number of letters added at most, and O(L) for the depth of the search.

2. 26 Slots a Node

Intuition

The same trie and the same search, its children held the other common way. Every word is lowercase English letters, so a node can keep an array of 26 children, one slot a letter, and find the child for a letter at the letter's distance from 'a'; an empty slot is a letter no added word takes there. addWord fills the empty slots on its path and marks the last node. search is the first approach's depth-first search: a letter steps to its one slot, a dot tries all 26 and answers true at the first that matches, and an empty slot is no match. A slot is read by its index where the first approach looks a letter up in a map; the price is that every node carries all 26 slots, used or not, and a dot visits the empty ones too.

Algorithm

1. A TrieNode holds children, an array of 26 slots, all empty at first, and is_end (Java: isEnd), false until a word ends there. The dictionary holds a root node.
2. addWord: from the root, for each letter, take its index (the letter minus 'a'); fill that slot with a new node if it is empty, and step down to it; mark the last node.
3. search: dfs(root, word, 0).
4. dfs(node, word, i): an empty slot (None, null) is false. At the end of the word, answer the node's mark. A dot: answer true if dfs(child, word, i + 1) is true for any of the 26 slots, and false if for none. A letter: dfs(the slot at its index, word, i + 1).

Time & Space

Time O(L) for addWord, L the length of its word, and O(L) for a search with no dot: one slot read a letter. Each dot tries all 26 slots at its depth, so a search with d dots is O(26^d x L) at most; LeetCode allows two. Space O(26 x N) for the nodes, N the number of letters added at most: each node holds 26 slots, however few of them it uses; and O(L) for the depth of the search.