-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0748-ShortestCompletingWord.cs
47 lines (42 loc) · 1.38 KB
/
0748-ShortestCompletingWord.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
//-----------------------------------------------------------------------------
// Runtime: 104ms
// Memory Usage: 27.9 MB
// Link: https://leetcode.com/submissions/detail/339610870/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _0748_ShortestCompletingWord
{
public string ShortestCompletingWord(string licensePlate, string[] words)
{
var target = CountWord(licensePlate.ToLower());
var result = string.Empty;
foreach (var word in words)
{
if (word.Length < result.Length || result.Length == 0)
{
var current = CountWord(word);
var match = true;
for (int i = 0; i < 26; i++)
{
if (current[i] < target[i])
{
match = false;
break;
}
}
if (match)
result = word;
}
}
return result;
}
private int[] CountWord(string word)
{
var count = new int[26];
foreach (var ch in word)
if (char.IsLetter(ch)) count[ch - 'a']++;
return count;
}
}
}