609. Sort Features by Popularity
Given a list of features and a list of responses (customer feedback). Each response contains words that may or may not be present in the features list.
The task is to reorder the features list based on their popularity in the responses.
The popularity of a feature is defined as the number of unique responses that mention it at least oncei i.e. one response could contain the same feature multiple times; this feature is only counted once in its popularity.
If two features have the same popularity, their original order in the features list should be maintained.
Example 1:
Input: features = ["cooler","lock","touch"], responses = ["i like cooler cooler","lock touch cool","locker like touch"]
Output: ["touch","cooler","lock"]
Explanation:
appearances("cooler") = 1, appearances("lock") = 1, appearances("touch") = 2. Since "cooler" and "lock" both had 1 appearance, "cooler" comes first because "cooler" came first in the features array.
Example 2:
Input: features = ["a","aa","b","c"], responses = ["a","a aa","a a a a a","b a"]
Output: ["a","aa","b","c"]
Explanation:
- appearances("a") = 4
- appearances("aa") = 1
- appearances("b") = 1
- appearances("c") = 0
Since "a" has the highest appearances, it comes first. "aa" and "b" both appear once, but "aa" appears earlier in the features list, so it comes before "b". "c" has 0 appearances and is placed last.
Now Your Turn!
Pick the correct output for the given inputInput: features = ["bh","be","bo","cd"], responses = ["a","a aa","a a a a a","b a"]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= features.length <= 104
- 1 <= features[i].length <= 10
- features contains no duplicates.
- features[i] consists of lowercase letters.
- 1 <= responses.length <= 102
- 1 <= responses[i].length <= 103
- responses[i] consists of lowercase letters and spaces.
- responses[i] contains no two consecutive spaces.
- responses[i] has no leading or trailing spaces.