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 leastkof each character, or return-1if it is not possible to takekof each character.
Input: s ="aabaaaacaabc", k =2Output: 8Explanation:
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 8is the minimum number of minutes needed.
Example 2:
1
2
3
Input: s ="a", k =1Output: -1Explanation: It is not possible to take one 'b' or 'c' so return-1.
Initial Counts: Count the occurrences of each character (‘a’, ‘b’, ‘c’) in the string s using a dictionary. If any character has less than k occurrences, it is impossible to meet the requirement, so return -1.
Sliding Window: Use a sliding window approach to find the smallest window that does not meet the requirement. Maintain a window of characters and adjust it until you have at least k occurrences of each character. Keep track of the minimum window size needed to achieve the goal.
Adjust Window:
Expand the window by including characters from the start of the string and decrement their counts.
If the count of any character goes below k, contract the window from the left until the count meets the requirement again.
Record the minimum window size that needs to be excluded.
classSolution {
publicinttakeCharacters(String s, int k) {
int n = s.length();
int left = 0, ans = n + 1, windowSz = 0;
Map<Character, Integer> charCnt =new HashMap<>();
// Count total occurrences of each characterfor (int i = 0; i < n; i++) {
charCnt.put(s.charAt(i), charCnt.getOrDefault(s.charAt(i), 0) + 1);
}
// If any character count is less than k, return -1if (charCnt.getOrDefault('a', 0) < k || charCnt.getOrDefault('b', 0) < k || charCnt.getOrDefault('c', 0) < k) {
return-1;
}
for (int right = 0; right < n; right++) {
char curr = s.charAt(right);
charCnt.put(curr, charCnt.get(curr) - 1);
windowSz++;
// Shrink the window until we fulfill the requirementwhile (charCnt.get(curr) < k) {
charCnt.put(s.charAt(left), charCnt.get(s.charAt(left)) + 1);
windowSz--;
left++;
}
ans = Math.min(ans, n - windowSz);
}
return ans;
}
}
classSolution:
deftakeCharacters(self, s: str, k: int) -> int:
length = len(s)
left, ans, windowSz =0, length +1, 0 charCnt: Dict[str, int] = {'a': 0, 'b': 0, 'c': 0}
# Count total occurrences of each characterfor char in s:
charCnt[char] +=1# If any character count is less than k, return -1if charCnt['a'] < k or charCnt['b'] < k or charCnt['c'] < k:
return-1for right in range(length):
curr = s[right]
charCnt[curr] -=1 windowSz +=1# Shrink the window until we fulfill the requirementwhile charCnt[curr] < k:
charCnt[s[left]] +=1 windowSz -=1 left +=1 ans = min(ans, length - windowSz)
return ans
⏰ Time complexity: O(n), where n is the length of the string s, since we process each character at most twice (once when expanding and once when contracting the window).
🧺 Space complexity: O(1), since the additional space used is constant (only a few variables and a fixed-size dictionary are used).