Word counts by prefix
Write a class PrefixCounter that keeps a collection of words in which the same word can be stored several times.
add(word)stores one more copy ofword.remove(word)deletes one copy ofword; if no copy is stored, it does nothing.count(word)returns how many copies of exactlywordare stored.count_prefix(prefix)returns how many stored words, copies included, start withprefix; a word equal toprefixcounts.
Words and prefixes use lowercase a–z and have 1 to 20 letters. Each operation should cost O(length of its argument), however many words are stored.
The tests call the provided run_counter(ops): it applies each operation in order and returns the list of results (None for add and remove, shown as null).
Besides the tests, one large input (15,000 operations) with a ~600 ms limit (scaled to your device's speed): brute force won't pass.
Examples
run_counter([["add","note"],["add","note"],["add","notes"],["count","note"],["count_prefix","not"],["remove","note"],["count","note"],["count_prefix","no"]])→[null,null,null,2,3,null,1,2]run_counter([["count","a"],["count_prefix","a"],["remove","a"],["add","a"],["count_prefix","a"],["count","a"]])→[0,0,null,null,1,1]run_counter([["add","car"],["count_prefix","cart"],["count_prefix","car"],["count","ca"],["count_prefix","c"],["add","cart"],["count_prefix","car"]])→[null,0,1,0,1,null,2]