-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1002-FindCommonCharacters.cs
40 lines (34 loc) · 1.1 KB
/
1002-FindCommonCharacters.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
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 32.2 MB
// Link: https://leetcode.com/submissions/detail/330724785/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _1002_FindCommonCharacters
{
public IList<string> CommonChars(string[] A)
{
var current = new int[26];
for (int i = 0; i < 26; i++)
current[i] = int.MaxValue;
foreach (var word in A)
{
var temp = new int[26];
foreach (var ch in word)
temp[ch - 'a']++;
for (int i = 0; i < 26; i++)
current[i] = Math.Min(current[i], temp[i]);
}
var result = new List<string>();
for (int i = 0; i < 26; i++)
{
while (current[i]-- > 0)
result.Add(((char)('a' + i)).ToString());
}
return result;
}
}
}