1. End of word
Consuming all characters succeeds only on a word terminal.
if (i === word.length) {
return node.isEnd;
}Typorithm · medium
Step 1 of 8 · intro card
Tries
Search with a '.' wildcard via DFS: an exact char follows one edge, while '.' branches into every child.
Adding a '.' wildcard turns trie search into a small DFS with branching.
An ordinary character follows its single edge, just like normal search.
A '.' must try every child, succeeding if any branch matches the rest of the word.
Recurse character by character.
'.' branches into all children.
Succeed at word end with isEnd.
search(word) {
const dfs = (node, i) => {
if (i === word.length) {
return node.isEnd;
}
const ch = word[i];
if (ch === ".") {
for (const child of node.children.values()) {
if (dfs(child, i + 1)) {
return true;
}
}
return false;
}
if (!node.children.has(ch)) {
return false;
}
return dfs(node.children.get(ch), i + 1);
};
return dfs(this, 0);
}Connect each part of the code to the concept before you type.
Consuming all characters succeeds only on a word terminal.
if (i === word.length) {
return node.isEnd;
}A dot matches any single character.
if (ch === ".") {Recurse into every child to satisfy the wildcard.
for (const child of node.children.values()) {A normal character follows its single edge.
return dfs(node.children.get(ch), i + 1)Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.