-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1002.cs
42 lines (36 loc) · 996 Bytes
/
Solution1002.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
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1002
{
/// <summary>
/// 1002. Find Common Characters - Easy
/// <a href="https://leetcode.com/problems/find-common-characters</a>
/// </summary>
public IList<string> CommonChars(string[] words)
{
int[] common = new int[26];
Array.Fill(common, int.MaxValue);
foreach (var word in words)
{
int[] count = new int[26];
foreach (var c in word)
{
count[c - 'a']++;
}
for (int i = 0; i < 26; i++)
{
common[i] = Math.Min(common[i], count[i]);
}
}
List<string> result = new();
for (char c = 'a'; c <= 'z'; c++)
{
int count = common[c - 'a'];
for (int i = 0; i < count; i++)
{
result.Add(c.ToString());
}
}
return result;
}
}