Prefix count queries
Given a list of words (possibly with duplicates) and a list of query strings, return for each query the number of words that have the query as a prefix. Build a trie where each node stores a counter of how many inserted words pass through it; then each query walks down the trie and reads the counter at its terminal node (0 if the path breaks). Return the list of prefix counts in query order.
Implement
prefix_count(words: list[str], queries: list[str]) → list[int]Examples
in
[["apple","app","apricot","banana"],["ap","app","b","xyz","apple"]]out[3,2,1,0,1]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 20 min
solution.py
InputExpectedGot
[["apple","app","apricot","banana"],["ap","app","b","xyz","apple"]][3,2,1,0,1]not run yetsample