-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1160-FindWordsThatCanBeFormedByCharacters.cs
43 lines (37 loc) · 1.18 KB
/
1160-FindWordsThatCanBeFormedByCharacters.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
//-----------------------------------------------------------------------------
// Runtime: 108ms
// Memory Usage: 36.1 MB
// Link: https://leetcode.com/submissions/detail/330701935/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _1160_FindWordsThatCanBeFormedByCharacters
{
public int CountCharacters(string[] words, string chars)
{
if (string.IsNullOrWhiteSpace(chars)) return 0;
var charMap = new int[26];
foreach (var ch in chars)
charMap[ch - 'a']++;
var result = 0;
var temp = new int[26];
foreach (var word in words)
{
charMap.CopyTo(temp, 0);
var isGood = true;
foreach (var ch in word)
{
if (temp[ch - 'a'] > 0)
temp[ch - 'a']--;
else
{
isGood = false;
break;
}
}
if (isGood) result += word.Length;
}
return result;
}
}
}