Class Solution
- java.lang.Object
-
- g2501_2600.s2516_take_k_of_each_character_from_left_and_right.Solution
-
public class Solution extends Object
2516 - Take K of Each Character From Left and Right.Medium
You are given a string
sconsisting of the characters'a','b', and'c'and a non-negative integerk. Each minute, you may take either the leftmost character ofs, or the rightmost character ofs.Return the minimum number of minutes needed for you to take at least
kof each character, or return-1if it is not possible to takekof 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 <= 105sconsists of only the letters'a','b', and'c'.0 <= k <= s.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
-
-
Method Detail
-
takeCharacters
public int takeCharacters(String s, int k)
-
-