Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sum of Prefix Scores of Strings

XPChallenge Points: 30
levelLevel: Hard

You are given an array words containing n non-empty strings.

For any substring that is a prefix of a word, its score is defined as the number of strings in the array that begin with that prefix.

Your task is to build an output array where each element represents the total score obtained from all prefixes of that particular word.

Every word contributes its own prefixes, and each prefix is counted as appearing in all words where it forms a starting portion.

Example 1:

Input: words = ["xya", "xy", "xa", "x"]

Output: [7, 6, 5, 4]

Explanation:

For "xya" → prefixes: "x", "xy", "xya" "x" appears in 4 words "xy" appears in 2 words "xya" appears in 1 word → total = 4 + 2 + 1 = 7 For "xy" → prefixes: "x", "xy" → 4 + 2 = 6 For "xa" → prefixes: "x", "xa" → 4 + 1 = 5 For "x" →

Example 2:

Input: words = ["hello"]

Output: [5]

Explanation:

Prefixes → "h", "he", "hel", "hell", "hello" Each prefix exists only once. → total score = 5

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result