Counting Words With a Given Prefix — Tries
Tally the saved searches that begin with the letters entered so far, counting a full match and repeats separately.
Tries · Easy
The problem
A search box shows how many saved searches begin with what the user has typed so far. You get a list of lowercase words and a prefix. Count how many words in the list start with that prefix. A word counts once for every place it appears in the list, and a word equal to the prefix counts too.
The story
A search box shows, as you type, how many saved searches begin with the letters typed so far. The saved searches are a list of up to 20 lowercase words, each 1 to 8 letters long, and the same word may be saved more than once. Given the letters typed, report how many saved searches start with exactly those letters. A saved search that equals the typed letters counts too.
What to return
- Return how many words in the list start with the prefix.
- Return 0 if none do.
Constraints
- 1 <= words.length <= 20
- each word and the prefix is 1 to 8 lowercase letters a-z
- words may repeat; each place in the list counts separately
Example
- Input
words = ["code", "cod", "coder", "decode"], prefix = "cod"- Output
3