-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0809-ExpressiveWords.cs
59 lines (51 loc) · 1.66 KB
/
0809-ExpressiveWords.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
57
58
59
//-----------------------------------------------------------------------------
// Runtime: 92ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0809_ExpressiveWords
{
public int ExpressiveWords(string S, string[] words)
{
(var keys, var counts) = CountString(S);
var result = 0;
foreach (var word in words)
{
(var wordKeys, var wordCounts) = CountString(word);
if (keys.Count != wordKeys.Count) continue;
var continueNext = false;
for (int i = 0; i < keys.Count; i++)
{
if (keys[i] != wordKeys[i] || counts[i] < wordCounts[i] || (counts[i] < 3 && counts[i] != wordCounts[i]))
{
continueNext = true;
break;
}
}
if (continueNext) continue;
result++;
}
return result;
}
private (IList<char> keys, IList<int> counts) CountString(string str)
{
var keys = new List<char>();
var counts = new List<int>();
for (int i = 0; i < str.Length; i++)
{
var count = 1;
while (i + 1 < str.Length && str[i] == str[i + 1])
{
count++;
i++;
}
keys.Add(str[i]);
counts.Add(count);
}
return (keys, counts);
}
}
}