Prefix cabinet: Positive-ink invoice
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. - Add only the numbers in
prefixTotalsthat are greater than 0. Store the sum inpositiveTotal. - Complete
prefixCabinetPositiveInkInvoiceso it returnspositiveTotal. Return it, do not print it.
Example
If words is ["slip", "slip", "slr", "binder"], then prefixTotals is [10, 10, 7, 6] and prefixCabinetPositiveInkInvoice returns 33.
Rules
- If
prefixTotalsis empty, return 0.
Test data
TEST DATA 1: words is ["key", "keyer", "kexb", "tray"].
TEST DATA 2: words is ["ledger", "ledger", "ledges"].
Examples
Input: words = ["key","keyer","kexb","tray"] Output: 30
prefixTotals is [8, 10, 8, 4]. Keep [8, 10, 8, 4] and add those numbers. Return 30.
Input: words = ["ledger","ledger","ledges"] Output: 50
prefixTotals is [17, 17, 16]. Keep [17, 17, 16] and add those numbers. Return 50.
Input: words = [] Output: 0
prefixTotals is []. Keep [] and add those numbers. 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.