2516. Take K of Each Character From Left and Right Medium

@problem@discussion
#Hash Table#String#Sliding Window



1/**
2 * [2516] Take K of Each Character From Left and Right
3 *
4 * 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.
5 * 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.
6 *  
7 * <strong class="example">Example 1:
8 * 
9 * Input: s = "aabaaaacaabc", k = 2
10 * Output: 8
11 * Explanation: 
12 * Take three characters from the left of s. You now have two 'a' characters, and one 'b' character.
13 * Take five characters from the right of s. You now have four 'a' characters, two 'b' characters, and two 'c' characters.
14 * A total of 3 + 5 = 8 minutes is needed.
15 * It can be proven that 8 is the minimum number of minutes needed.
16 * 
17 * <strong class="example">Example 2:
18 * 
19 * Input: s = "a", k = 1
20 * Output: -1
21 * Explanation: It is not possible to take one 'b' or 'c' so return -1.
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	1 <= s.length <= 10^5
27 * 	s consists of only the letters 'a', 'b', and 'c'.
28 * 	0 <= k <= s.length
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/
34// discuss: https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn take_characters(s: String, k: i32) -> i32 {
40        0
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_2516() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.