Description#
You are given a string s
consisting of the characters 'a'
, 'b'
, and 'c'
and a non-negative integer k
. Each minute, you may take either the leftmost character of s
, or the rightmost character of s
.
Return the minimum number of minutes needed for you to take at least k
of each character, or return -1
if it is not possible to take k
of each character.
Example 1:
Input: s = "aabaaaacaabc", k = 2
Output: 8
Explanation:
Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
A total of 3 + 5 = 8 minutes is needed.
It can be proven that 8 is the minimum number of minutes needed.
Example 2:
Input: s = "a", k = 1
Output: -1
Explanation: It is not possible to take one 'b' or 'c' so return -1.
Constraints:
1 <= s.length <= 105
s
consists of only the letters 'a'
, 'b'
, and 'c'
.0 <= k <= s.length
Solutions#
Solution 1#
1
2
3
4
5
6
7
8
9
10
11
12
13
| class Solution:
def takeCharacters(self, s: str, k: int) -> int:
cnt = Counter(s)
if any(cnt[c] < k for c in "abc"):
return -1
ans = j = 0
for i, c in enumerate(s):
cnt[c] -= 1
while cnt[c] < k:
cnt[s[j]] += 1
j += 1
ans = max(ans, i - j + 1)
return len(s) - ans
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Solution {
public int takeCharacters(String s, int k) {
int[] cnt = new int[3];
int n = s.length();
for (int i = 0; i < n; ++i) {
++cnt[s.charAt(i) - 'a'];
}
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) {
return -1;
}
int ans = 0, j = 0;
for (int i = 0; i < n; ++i) {
int c = s.charAt(i) - 'a';
--cnt[c];
while (cnt[c] < k) {
++cnt[s.charAt(j++) - 'a'];
}
ans = Math.max(ans, i - j + 1);
}
return n - ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| class Solution {
public:
int takeCharacters(string s, int k) {
int cnt[3] = {0};
for (char& c : s) ++cnt[c - 'a'];
if (cnt[0] < k || cnt[1] < k || cnt[2] < k) return -1;
int ans = 0, j = 0;
int n = s.size();
for (int i = 0; i < n; ++i) {
int c = s[i] - 'a';
--cnt[c];
while (cnt[c] < k) {
++cnt[s[j++] - 'a'];
}
ans = max(ans, i - j + 1);
}
return n - ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| func takeCharacters(s string, k int) int {
cnt := [3]int{}
for _, c := range s {
cnt[c-'a']++
}
if cnt[0] < k || cnt[1] < k || cnt[2] < k {
return -1
}
ans, j := 0, 0
for i, c := range s {
c -= 'a'
cnt[c]--
for cnt[c] < k {
cnt[s[j]-'a']++
j++
}
ans = max(ans, i-j+1)
}
return len(s) - ans
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| function takeCharacters(s: string, k: number): number {
const getIndex = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
const count = [0, 0, 0];
for (const c of s) {
count[getIndex(c)]++;
}
if (count.some(v => v < k)) {
return -1;
}
const n = s.length;
let ans = 0;
for (let i = 0, j = 0; j < n; j++) {
count[getIndex(s[j])]--;
while (count[getIndex(s[j])] < k) {
count[getIndex(s[i])]++;
i++;
}
ans = Math.max(ans, j - i + 1);
}
return n - ans;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| impl Solution {
pub fn take_characters(s: String, k: i32) -> i32 {
let s = s.as_bytes();
let mut count = vec![0; 3];
for c in s.iter() {
count[(c - b'a') as usize] += 1;
}
if count.iter().any(|v| *v < k) {
return -1;
}
let n = s.len();
let mut ans = 0;
let mut i = 0;
for j in 0..n {
count[(s[j] - b'a') as usize] -= 1;
while count[(s[j] - b'a') as usize] < k {
count[(s[i] - b'a') as usize] += 1;
i += 1;
}
ans = ans.max(j - i + 1);
}
(n - ans) as i32
}
}
|