-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution809.cs
64 lines (52 loc) · 1.3 KB
/
Solution809.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
60
61
62
63
64
using System.Text;
namespace LeetCode.Solutions;
public class Solution809
{
/// <summary>
/// 809. Expressive Words - Medium
/// <a href="https://leetcode.com/problems/expressive-words">See the problem</a>
/// </summary>
public int ExpressiveWords(string s, string[] words)
{
var result = 0;
foreach (var word in words)
{
if (IsExpressive(s, word))
{
result++;
}
}
return result;
}
private bool IsExpressive(string s, string word)
{
var i = 0;
var j = 0;
while (i < s.Length && j < word.Length)
{
if (s[i] != word[j])
{
return false;
}
var countS = 1;
var countWord = 1;
while (i + 1 < s.Length && s[i] == s[i + 1])
{
i++;
countS++;
}
while (j + 1 < word.Length && word[j] == word[j + 1])
{
j++;
countWord++;
}
if (countS < countWord || (countS != countWord && countS < 3))
{
return false;
}
i++;
j++;
}
return i == s.Length && j == word.Length;
}
}