-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution395.cs
66 lines (52 loc) · 1.54 KB
/
Solution395.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
65
66
using System.Text;
namespace LeetCode.Solutions;
public class Solution395
{
/// <summary>
/// 395. Longest Substring with At Least K Repeating Characters - Medium
/// <a href="https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters">See the problem</a>
/// </summary>
public int LongestSubstring(string s, int k)
{
var count = new int[26];
var result = 0;
for (var i = 1; i <= 26; i++)
{
Array.Fill(count, 0);
int unique = 0;
int noLessThanK = 0;
for (int start = 0, end = 0; end < s.Length; end++)
{
var index = s[end] - 'a';
if (count[index] == 0)
{
unique++;
}
count[index]++;
if (count[index] == k)
{
noLessThanK++;
}
while (unique > i)
{
index = s[start] - 'a';
if (count[index] == k)
{
noLessThanK--;
}
count[index]--;
if (count[index] == 0)
{
unique--;
}
start++;
}
if (unique == noLessThanK)
{
result = Math.Max(result, end - start + 1);
}
}
}
return result;
}
}