Top K Frequently Mentioned Keywords

Given a list of reviews, a list of keywords and an integer k. Find the most popular k keywords in order of most to least frequently mentioned.

The comparison of strings is case-insensitive.

Multiple occurances of a keyword in a review should be considred as a single mention.

If keywords are mentioned an equal number of times in reviews, sort alphabetically.


The input consists of three arguments:

k: an integer

keywords: a list of strings representing the keywords

reviews: a list of strings that consists of space-sperated words representing sentences


Return a list of strings of most popular k keywords in order of most to least frequently mentioned


Example 1:


k = 2

keywords = ["anacell", "cetracular", "betacellular"]
reviews = [
  "Anacell provides the best services in the city",
  "betacellular has awesome services",
  "Best services provided by anacell, everyone should use anacell",

Output: ["anacell", "betacellular"]


anacell is occuring in 2 different reviews and betacellular is only occuring in 1 review.


Example 2:


k = 2

keywords = ["anacell", "betacellular", "cetracular", "deltacellular", "eurocell"]
reviews = [
  "I love anacell Best services; Best services provided by anacell",
  "betacellular has great services",
  "deltacellular provides much better services than betacellular",
  "cetracular is worse than anacell",
  "Betacellular is better than deltacellular.",

Output: ["betacellular", "anacell"]


betacellular is occuring in 3 different reviews. anacell and deltacellular are occuring in 2 reviews, but anacell is lexicographically smaller.

Try it yourself