-
Notifications
You must be signed in to change notification settings - Fork 112
/
1170-CompareStringsByFrequencyOfTheSmallestCharacter.cs
52 lines (46 loc) · 1.57 KB
/
1170-CompareStringsByFrequencyOfTheSmallestCharacter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//-----------------------------------------------------------------------------
// Runtime: 244ms
// Memory Usage: 33.3 MB
// Link: https://leetcode.com/submissions/detail/337648929/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _1170_CompareStringsByFrequencyOfTheSmallestCharacter
{
public int[] NumSmallerByFrequency(string[] queries, string[] words)
{
var count = new int[12];
foreach (var word in words)
{
var charCount = new int[26];
var minChar = 26;
foreach (var ch in word)
{
var index = ch - 'a';
charCount[index]++;
if (index < minChar)
minChar = index;
}
count[charCount[minChar]]++;
}
for (int i = count.Length - 1; i > 0; i--)
count[i - 1] += count[i];
var result = new int[queries.Length];
var queryIndex = 0;
foreach (var query in queries)
{
var charCount = new int[26];
var minChar = 26;
foreach (var ch in query)
{
var index = ch - 'a';
charCount[index]++;
if (index < minChar)
minChar = index;
}
result[queryIndex++] = count[charCount[minChar] + 1];
}
return result;
}
}
}