-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution916.cs
56 lines (49 loc) · 1.28 KB
/
Solution916.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
53
54
55
56
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution916
{
/// <summary>
/// 916. Word Subsets - Medium
/// <a href="https://leetcode.com/problems/word-subsets">See the problem</a>
/// </summary>
public IList<string> WordSubsets(string[] words1, string[] words2)
{
var result = new List<string>();
var maxCount = new int[26];
foreach (var word in words2)
{
var count = GetCharCount(word);
for (var i = 0; i < 26; i++)
{
maxCount[i] = Math.Max(maxCount[i], count[i]);
}
}
foreach (var word in words1)
{
var count = GetCharCount(word);
var isUniversal = true;
for (var i = 0; i < 26; i++)
{
if (count[i] < maxCount[i])
{
isUniversal = false;
break;
}
}
if (isUniversal)
{
result.Add(word);
}
}
return result;
}
private int[] GetCharCount(string word)
{
var count = new int[26];
foreach (var ch in word)
{
count[ch - 'a']++;
}
return count;
}
}