Ctrl K

Prefix Popularity Totals

Hard
A tag search suggests completions as you type. The popularity of a string p is the number of tags in tags that start with p (a tag counts as starting with itself). For every tag, add up the popularity of each of its non-empty prefixes, from the first letter up to the whole tag. Return these totals in the same order as tags. Duplicate tags are counted separately.
Example 1
tags=["cat", "car", "dog"] → [5, 5, 3]

cat has the prefixes c (shared by 2 tags), ca (2) and cat (1): 2 + 2 + 1 = 5. Same for car. Every prefix of dog belongs to it alone: 1 + 1 + 1 = 3.

Example 2
tags=["go", "gopher", "g"] → [5, 9, 3]

The prefix g is shared by all three tags, go by two, and the rest of gopher's prefixes by it alone. For go: 3 + 2 = 5; for gopher: 3 + 2 + 1 + 1 + 1 + 1 = 9; for g: 3.

Example 3
tags=["zz", "zz"] → [4, 4]
Constraints
  • 1 ≤ len(tags) ≤ 1000
  • 1 ≤ len(tags[i]) ≤ 1000
  • Tags consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Searched Words MediumEvery Way to Space the Words HardWords Hidden in a Letter Grid HardGlued Into a Palindrome HardMirror Word Blocks Hard
loading Python…
Input
tags=["cat", "car", "dog"]
Expected
[5, 5, 3]

Run checks the first 3 tests in your browser. Submit checks all 99 tests on the server, hidden ones included.