Problem Description
Given an array of feature names and an array of user responses (each response being a string of space-separated words), calculate the popularity of each feature as the number of responses that mention it at least once. Then, return the list of features sorted in non-increasing order by popularity. If two features have the same popularity, preserve their original order from the features array.
Key Insights
- Use a set to store words from each response to avoid counting duplicate mentions in the same response.
- Use a dictionary (or hash map) to count how many responses mention each feature.
- Preserve the original index to handle ties by sorting features with equal counts by their order in the input list.
- Sorting involves first sorting by popularity (in descending order) and then by the original index (in ascending order).
Space and Time Complexity
Time Complexity: O(R * L + F log F)
- R: number of responses.
- L: average number of words per response.
- F: number of features. Space Complexity: O(F + W)
- F: space for the features count dictionary.
- W: auxiliary space for the word sets per response (processed one at a time).
Solution
We first create a mapping from each feature name to its corresponding index and initialize a count dictionary for popularity. For each response, split the string into words and convert them into a set to remove duplicates. Then, for every feature in the set that also appears in the features list, increment its count. Finally, sort the features based on their popularity in descending order; if counts are equal, use the original index (ascending order) to decide the order.