Sort Features by Popularity
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 276 ms)β
| Metric | Value |
|---|---|
| Runtime | 276 ms |
| Memory | 50 MB |
| Date | 2022-01-24 |
Solution
public class Solution {
public string[] SortFeatures(string[] features, string[] responses) {
Dictionary<string, int> freq = new Dictionary<string, int>();
foreach(var word in features)
{
freq.Add(word, 0);
}
foreach (var response in responses)
{
HashSet<string> arr = new HashSet<string>(response.Split(' '));
foreach (var res in arr)
{
if(freq.ContainsKey(res))
{
freq[res]++;
}
}
}
return freq.OrderByDescending(x => x.Value).Select(x => x.Key).ToArray();
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |