-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
42 lines (38 loc) · 987 Bytes
/
Solution.cs
File metadata and controls
42 lines (38 loc) · 987 Bytes
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
public class Solution
{
public int MaximumLength(string s)
{
int[] chars = new int[26];
foreach (char c in s)
{
chars[c - 'a']++;
}
int max = 0;
for (int i = 0; i < 26; i++)
{
max = Math.Max(max, chars[i]);
}
if (max < 3) return -1;
for (int len = max - 2; len > 1; len--)
{
Dictionary<string, int> freq = [];
int st = 0, ed = 1;
while (ed < s.Length)
{
if (s[st] != s[ed])
{
st = ed;
}
if (ed - st + 1 == len)
{
string key = s[st..ed];
freq[key] = freq.GetValueOrDefault(key, 0) + 1;
st++;
}
ed++;
}
if (freq.Count > 0 && freq.Max(x => x.Value) >= 3) return len;
}
return 1;
}
}