Prefix cabinet: Even-key census
Stuck or curious how this works? Visualize Binary Heap Operations ↗
Inputs
words is a list of words.
Your tasks
- Create a list
prefixTotals. A prefix is the start of a word. For example, "c", "ca", and "cat" are the prefixes with at least one letter of "cat". For each word inwords, look at all its prefixes with at least one letter. Count how many inputwordsstart with each prefix, then add those counts. Count repeated inputwordsseparately, and include the word itself. Put one total per word inprefixTotals, in input order. - Count the even numbers in
prefixTotals. Store the count inevenCount. A number is even when it divides by 2 with no remainder. - Complete
prefixCabinetEvenKeyCensusso it returnsevenCount. Return it, do not print it.
Example
If words is ["slip", "slip", "sle", "key"], then prefixTotals is [10, 10, 7, 3] and prefixCabinetEvenKeyCensus returns 2.
Rules
- If
prefixTotalsis empty, return 0.
Test data
TEST DATA 1: words is ["binder", "bined", "bipr", "stamp"].
TEST DATA 2: words is ["margin", "margin", "margil"].
Examples
Input: words = ["binder","bined","bipr","stamp"] Output: 2
prefixTotals is [11, 10, 8, 5]. The even numbers are [10, 8]. There are 2, so return 2.
Input: words = ["margin","margin","margil"] Output: 1
prefixTotals is [17, 17, 16]. The even numbers are [16]. There are 1, so return 1.
Input: words = [] Output: 0
prefixTotals is []. The even numbers are []. There are 0, so return 0.
Input limits
- 0 <= input size <= 64.
- Answers fit in a signed 32-bit integer: -2,147,483,648 through 2,147,483,647.
- There are at most 64 lowercase English words, each with 1 to 12 letters.
Efficiency goal
Big-O describes how the work or memory needed grows as the input gets larger.
Efficiency goal: time O(total characters); extra memory, including the helper list, O(n). n is the input length; total characters means all word lengths added together. These labels describe how work and memory grow with the input.
Practice focus
Build prefixTotals first, then return exactly what the tasks ask for. Follow the order, keep repeated values when the rule says so, and check the empty case.