Favorite Genres
EasyHash Map · Amazon
You are given two maps:
user_songs: user name -> list of songs that user listened to. A song listed twice for a user counts as two listens.song_genres: genre name -> list of songs in that genre. Each song belongs to at most one genre; songs that are in no genre are ignored.
For each user, count their listens per genre. The user's favorite genres are all genres tied for that user's highest count.
Return a map from every user in user_songs to the list of their favorite genres, sorted alphabetically. If none of a user's listens belong to any genre (including a user with no songs), map that user to an empty list.
Example 1
- Input:
- user_songs = {'Mia': ['s1', 's4', 's5', 's1'], 'Leo': ['s2', 's3', 's6']}, song_genres = {'Indie': ['s1', 's2'], 'Soul': ['s3', 's4'], 'Folk': ['s5', 's6']}
- Output:
- {'Mia': ['Indie'], 'Leo': ['Folk', 'Indie', 'Soul']}
- Explanation
- Mia listened to s1 twice, so Indie has 2 listens against 1 each for Soul and Folk. Leo has exactly one listen in each of the three genres, so all three tie and are returned in alphabetical order.
Example 2
- Input:
- user_songs = {'Ava': ['x', 'y'], 'Ben': ['z', 'q', 'z']}, song_genres = {'Rock': ['z'], 'Jazz': ['w']}
- Output:
- {'Ava': [], 'Ben': ['Rock']}
- Explanation
- None of Ava's songs belong to a genre, so she maps to an empty list. Ben's two listens of z are Rock; q has no genre and is ignored.
Constraints
0 <= number of users, number of genres <= 10^4- The total length of all song lists in both maps is at most
10^5 - Each song appears in at most one genre list