Formatted question description: https://leetcode.ca/all/1772.html

1772. Sort Features by Popularity

Level

Medium

Description

You are given a string array features where features[i] is a single word representing the name of a feature of the latest product you are working on. You made a survey where users reported which features they like. You are given a string array responses, where responses[i] is a string containing space-separated words.

You want to sort the features according to their popularity. More formally, let appearances(word) be the number of is such that responses[i] contains word as a word. Then the x-th feature is more popular than the y-th feature if appearances(features[x]) > appearances(features[y]).

Return an array sortedFeatures consisting of the feature names sorted by their popularity. If the x-th and y-th features have the same popularity where x < y, then you should put the x-th feature before the y-th one.

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”]

Constraints:

  • 1 <= features.length <= 10^4
  • 1 <= features[i].length <= 10
  • features contains no duplicates.
  • features[i] consists of lowercase letters.
  • 1 <= responses.length <= 10^2
  • 1 <= responses[i].length <= 10^3
  • responses[i] consists of lowercase letters and spaces.
  • responses[i] contains no two consecutive spaces.
  • responses[i] has no leading or trailing spaces.

Solution

Use two maps to store the indices of each feature in features, and the appearances of each feature in responses. Loop over features and responses to put the value into the two maps. Then sort features first according to the appearances in descending order next according to the indices in ascending order. Return the sorted array features.

class Solution {
    public String[] sortFeatures(String[] features, String[] responses) {
        Map<String, Integer> indicesMap = new HashMap<String, Integer>();
        int featuresCount = features.length;
        for (int i = 0; i < featuresCount; i++)
            indicesMap.put(features[i], i);
        Map<String, Integer> appearancesMap = new HashMap<String, Integer>();
        int length = responses.length;
        for (int i = 0; i < length; i++) {
            String response = responses[i];
            String[] array = response.split(" ");
            Set<String> set = new HashSet<String>();
            for (String word : array)
                set.add(word);
            for (String feature : features) {
                if (set.contains(feature)) {
                    int count = appearancesMap.getOrDefault(feature, 0) + 1;
                    appearancesMap.put(feature, count);
                }
            }
        }
        Arrays.sort(features, new Comparator<String>() {
            public int compare(String str1, String str2) {
                int appearances1 = appearancesMap.getOrDefault(str1, 0);
                int appearances2 = appearancesMap.getOrDefault(str2, 0);
                if (appearances1 != appearances2)
                    return appearances2 - appearances1;
                else
                    return indicesMap.get(str1) - indicesMap.get(str2);
            }
        });
        return features;
    }
}

All Problems

All Solutions