Ctrl K

Dishes by Mentions

Medium
A café has collected guest reviews. The names of the dishes on its menu are in the array dishes (each one is a single word, no duplicates), and the reviews are in the array reviews: each review is a sequence of words separated by single spaces. The popularity of a dish is the number of reviews in which its name appears as a separate word. If a review names the same dish several times, that review still counts only once. Return the dish names ordered by popularity, highest first. Dishes with equal popularity keep the order they have in dishes.
Example 1
dishes=["soup", "pie", "salad"], reviews=["pie pie pie", "soup and pie", "salads were cold"] → ['pie', 'soup', 'salad']

pie is mentioned in two reviews (repeats within one review count once), soup in one, salad in none: the word salads is not salad.

Example 2
dishes=["tea", "cake", "jam"], reviews=["jam", "cake"] → ['cake', 'jam', 'tea']

cake and jam have one mention each and keep their order from dishes; tea is never mentioned, so it goes last.

Example 3
dishes=["rice"], reviews=["more rice please"] → ['rice']
Constraints
  • 1 ≤ len(dishes) ≤ 10⁴
  • 1 ≤ len(dishes[i]) ≤ 20; names consist of lowercase English letters, digits and hyphens
  • All dishes[i] are distinct
  • 1 ≤ len(reviews) ≤ 100
  • 1 ≤ len(reviews[j]) ≤ 1000
  • reviews[j] consists of lowercase English letters, digits, hyphens and spaces, with no leading or trailing spaces and no two spaces in a row
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAnagram Buckets MediumA Subsequence of One Word Only MediumMost Searched Words MediumWord Grown Letter by Letter MediumMerge Contact Cards Medium
loading Python…
Input
dishes=["soup", "pie", "salad"], reviews=["pie pie pie", "soup and pie", "salads were cold"]
Expected
['pie', 'soup', 'salad']

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