Medium · Strings

Sum of Prefix Scores of Strings

Given an array words of n non-empty lowercase strings, return an array answer of length n where answer[i] is the sum, over every non-empty prefix p of words[i], of the number of words that start with p (a word counts as a prefix of itself).

Examples

Example 1

["abc","ab","bc","b"]

Output: [5,4,3,2]

Example 2

["abcd","ab","bc","cba"]

Output: [6,4,2,3]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Strings problems