Dsa
Trie
Hard
Incremental Registry Typing Cost
You are building a registry. When a new word is added, its 'Typing Cost' is the length of the shortest prefix of that word that has not appeared as a prefix of any word already in the registry. If every prefix of the new word is already present in the registry (i.e., the word is a prefix of an existing entry), the cost is the full length of the word. Calculate the total typing cost for a sequence of words as they are added one by one.
Input Format: A sequence of space-separated strings.
Output Format: A single integer representing the sum of costs.
Example:
Input: apple apply apple
Output: 11
Explanation:
1. 'apple': Registry empty. Shortest unique prefix is 'a' (length 1). Registry: {apple}
2. 'apply': 'a', 'ap', 'app', 'appl' are already prefixes of 'apple'. 'apply' is unique (length 5). Registry: {apple, apply}
3. 'apple': All prefixes already exist. Cost is full length (length 5).
Total: 1 + 5 + 5 = 11.
Key concepts
incremental-trieunique-prefixes
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now