Fundamentals/Autocomplete
← PrevNext →
Words arrive one at a time. After each word is added to the dictionary, count how many characters the user must type before autocomplete uniquely identifies it. A prefix is unique when it matches exactly one dictionary word. If the word is itself a prefix of another already-inserted word, autocomplete never becomes unique and the user must type every character. Return the total keystrokes summed across all insertions.
Input
words: an array of distinct lowercase strings, processed in order
Output
The total number of keystrokes required across all words.
Examples
Example 1
Inputwords = ["cat", "dog", "bird"]
Output3
Explanation: Each word's first letter is unique at insertion time (1 + 1 + 1).
Example 2
Inputwords = ["hi", "hello", "bojack", "hills", "hill"]
Output11
Explanation: 1 + 2 + 1 + 3 + 4. "hill" is a prefix of "hills" so all 4 chars are typed.
Constraints
1 <= words.length <= 100001
Total length of all strings <= 100001
Lowercase letters only; no duplicates.