← Hash Strategies

Micro-Drill #257 — Trie search with wildcard DFS

Hash Strategies Target: 15s

Wildcard '.' branches to all children. Without wildcards it's a straight walk — the DFS only fires on '.' characters.

def search(node, word, i=0):
    if i == len(word):
        return node.is_end
    if word[i] == '.':
        return any(search(ch, word, i+1)
                   for ch in node.children.values())
    if word[i] not in node.children:
        return False
    return search(node.children[word[i]], word, i+1)

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #256 Micro #258 →