leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2516.cpp (586B)
0 class Solution {
1 public:
2 int takeCharacters(const string &s, int k) const {
3 const int n = size(s);
4 int count[3] = {0};
6 for (const char c : s)
7 count[c - 'a']++;
8 if (count[0] < k || count[1] < k || count[2] < k) return -1;
10 int res = n;
11 for (int i = n - 1, j = n - 1; i >= 0; i--) {
12 count[s[i] - 'a']--;
14 while (count[0] < k || count[1] < k || count[2] < k) {
15 count[s[j--] - 'a']++;
16 }
18 res = min(res, i - j);
19 }
21 return res + n - 1;
22 }
23 };