Imagine you're working for a search engine optimization company and need to analyze how popular different word prefixes are across a collection of terms. Given an array of words, your task is to calculate prefix popularity scores.
The score of any string prefix is defined as the number of words in the array that have this prefix. For example, if we have ["apple", "app", "application", "cat"], then the prefix "app" has a score of 3 because it appears at the beginning of "apple", "app", and "application".
Your goal is to return an array where each element represents the sum of scores of all possible non-empty prefixes for the corresponding word. Remember, a word is considered a prefix of itself!
Example: For words = ["abc", "ab", "bc", "b"]
• Word "abc" has prefixes: "a" (score=1), "ab" (score=2), "abc" (score=1) → total = 4
• Word "ab" has prefixes: "a" (score=1), "ab" (score=2) → total = 3
• And so on...
Input & Output
Constraints
- 1 ≤ words.length ≤ 1000
- 1 ≤ words[i].length ≤ 1000
- words[i] consists of lowercase English letters
- Sum of all words[i].length ≤ 2 × 105