Prefix cabinet: Threshold file
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 numbers in
prefixTotalsthat are at leastk(greater than or equal tok). Store the count inatLeastK. - Complete
prefixCabinetThresholdFileso it returnsatLeastK. Return it, do not print it.
Example
If words is ["margin", "margin", "mad", "paper"] and k is 7, then prefixTotals is [14, 14, 7, 5] and prefixCabinetThresholdFile returns 3.
Rules
- If
prefixTotalsis empty, the count is 0. k(a whole number from 1 to 8) is the threshold in task 2: compare each number with it.
Test data
TEST DATA 1: words is ["slip", "sliside", "slco", "roller"] and k is 8.
TEST DATA 2: words is ["binder", "binder", "bindel"] and k is 3.
Examples
Input: words = ["slip","sliside","slco","roller"] k = 8 Output: 3
prefixTotals is [9, 12, 8, 6]. Here k is 8. The numbers at least 8 are [9, 12, 8]. There are 3, so return 3.
Input: words = ["binder","binder","bindel"] k = 3 Output: 3
prefixTotals is [17, 17, 16]. Here k is 3. The numbers at least 3 are [17, 17, 16]. There are 3, so return 3.
Input: words = [] k = 5 Output: 0
prefixTotals is []. Here k is 5. The numbers at least 5 are []. There are 0, so return 0.
Input limits
- 0 <= input size <= 64.
- k is a whole number from 1 to 8. 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.