Skip to contentKEEP THE CRAFTTHE SANCTUARY

Follow Trie Edges

Build a trie and measure how far each query follows it from the root.

The problem

Implement trie_prefix_match_lengths(words, queries). For each query, find the length of its longest prefix that is also a prefix of at least one word in words (0 if even its first character starts no word). The query does not have to be a complete word, and the empty string counts as length 0. Return the lengths in the same order as queries. Build a trie from words so that each query takes time proportional to its own length.

Starter code

def trie_prefix_match_lengths(words, queries):
    pass

Public examples

  1. words = ["cat", "car", "dog"], queries = ["cart", "cap", "do", "z"]gives[3, 2, 2, 0]
  2. words = ["abc"], queries = [""]gives[0]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.