Top Mentioned Competitors
MediumHash Map · Amazon
You are given a list of competitor names competitors (distinct, lowercase letters and digits), a list of customer reviews, and an integer k.
To read a review, lowercase it and split it into words, where a word is a maximal run of letters a-z and digits 0-9; every other character (spaces, punctuation, -, _, and so on) separates words. A review mentions a competitor if one of its words is exactly that competitor's name, so "pineapple" does not mention "apple".
The score of a competitor is the number of reviews that mention it. A review counts at most once per competitor, however many times the name appears in it.
Return the k competitors with the highest scores, ordered by score from highest to lowest, with equal scores ordered alphabetically. Only competitors with a score of at least 1 may be returned, so the result can be shorter than k.
Example 1
- Input:
- reviews = ["Shipping from Ebay was slow", "ebay and etsy both had it", "Etsy, etsy, ETSY!", "I prefer walmart"], competitors = ["walmart", "etsy", "ebay", "target"], k = 2
- Output:
- ["ebay", "etsy"]
- Explanation
ebayappears in the first two reviews andetsyin the second and third (the three mentions in the third review count once), so both score 2.walmartscores 1. The tie betweenebayandetsyis broken alphabetically.
Example 2
- Input:
- reviews = ["target target target", "bought it at walmart"], competitors = ["target", "walmart"], k = 2
- Output:
- ["target", "walmart"]
- Explanation
targetappears three times but in a single review, so it scores 1, the same aswalmart. The tie is broken alphabetically.
Example 3
- Input:
- reviews = ["the new console sold out", "pineapple stand"], competitors = ["apple", "newegg"], k = 3
- Output:
- []
- Explanation
- No review contains
appleorneweggas a whole word (pineappleis a different word), so nothing is returned even thoughkis 3.
Constraints
0 <= reviews.length <= 10^40 <= reviews[i].length <= 10001 <= competitors.length <= 10^4, names are distinct, lowercase, alphanumeric, and at most 20 characters0 <= k <= competitors.length