Sum of Prefix Scores of Strings

HardStringstrie

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an array words of non-empty strings. The **score** of a string t is the number of entries in words that have t as a prefix (a string is a prefix of itself, and equal entries are counted separately). For each words[i], add up the scores of all of its non-empty prefixes. Return an array answer where answer[i] is that total.

Examples

Input: words = ["abc","ab","bc","b"]
Output: [5,4,3,2]
For "abc": "a" scores 2, "ab" scores 2, "abc" scores 1, total 5. For "b": "b" scores 2.
Input: words = ["abcd"]
Output: [4]
Each of the 4 prefixes scores 1.

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 1000
  • words[i] consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More trie problems