Sum of Prefix Scores of Strings

Hard Topics Company Tags

For each nonempty prefix of a word, its score is the number of entries in words that begin with that prefix. Return an array where the entry for each word is the sum of the scores of all its nonempty prefixes. Include the whole word as one of its prefixes. Count repeated words separately and return answers in the original order.

Example 1:

Input: words = ["ax","ay","b"]

Output: [3,3,1]

Explanation: Prefix a occurs in two words, while ax and ay each occur in one. Their totals are 2+1=3 each. Prefix b occurs in only its own word, giving 1.

Example 2:

Input: words = ["go","go"]

Output: [4,4]

Explanation: Both g and go begin both entries. Each prefix has score 2, so each word has total 2+2=4. Repeated entries count separately.

Example 3:

Input: words = ["cat"]

Output: [3]

Explanation: The nonempty prefixes are c, ca, and cat. Each begins exactly one word, giving 1+1+1=3.

Constraints:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • Every word contains only lowercase English letters.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.