-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (33 loc) · 843 Bytes
/
Solution.cs
File metadata and controls
36 lines (33 loc) · 843 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
public class Solution
{
public int TakeCharacters(string s, int k)
{
if (k == 0) return 0;
int[] map = new int['c' - 'a' + 1];
foreach (var c in s)
{
map[c - 'a']++;
}
if (map.Any(x => x < k)) return -1;
int left = 0;
int right = 0;
while (right < s.Length && map[s[right] - 'a'] > k)
{
map[s[right] - 'a']--;
right++;
}
int min = s.Length - right;
while (left < s.Length)
{
map[s[left] - 'a']++;
while (right < s.Length && map[s[right] - 'a'] > k)
{
map[s[right] - 'a']--;
right++;
}
min = Math.Min(min, left + 1 + s.Length - right);
left++;
}
return min;
}
}