2516. Take K of Each Character From Left and Right LeetCode Solution

In this guide, you will get 2516. Take K of Each Character From Left and Right LeetCode Solution with the best time and space complexity. The solution to Take K of Each Character From Left and Right problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Take K of Each Character From Left and Right solution in C++
  4. Take K of Each Character From Left and Right solution in Java
  5. Take K of Each Character From Left and Right solution in Python
  6. Additional Resources
2516. Take K of Each Character From Left and Right LeetCode Solution image

Problem Statement of Take K of Each Character From Left and Right

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.

See also  3146. Permutation Difference between Two Strings LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2516. Take K of Each Character From Left and Right LeetCode Solution in C++

class Solution {
 public:
  int takeCharacters(string s, int k) {
    const int n = s.length();
    int ans = n;
    vector<int> count(3);

    for (const char c : s)
      ++count[c - 'a'];

    if (count[0] < k || count[1] < k || count[2] < k)
      return -1;

    for (int l = 0, r = 0; r < n; ++r) {
      --count[s[r] - 'a'];
      while (count[s[r] - 'a'] < k)
        ++count[s[l++] - 'a'];
      ans = min(ans, n - (r - l + 1));
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2516. Take K of Each Character From Left and Right LeetCode Solution in Java

class Solution {
  public int takeCharacters(String s, int k) {
    final int n = s.length();
    int ans = n;
    int[] count = new int[3];

    for (final char c : s.toCharArray())
      ++count[c - 'a'];

    if (count[0] < k || count[1] < k || count[2] < k)
      return -1;

    for (int l = 0, r = 0; r < n; ++r) {
      --count[s.charAt(r) - 'a'];
      while (count[s.charAt(r) - 'a'] < k)
        ++count[s.charAt(l++) - 'a'];
      ans = Math.min(ans, n - (r - l + 1));
    }

    return ans;
  }
}
// code provided by PROGIEZ

2516. Take K of Each Character From Left and Right LeetCode Solution in Python

class Solution:
  def takeCharacters(self, s: str, k: int) -> int:
    n = len(s)
    ans = n
    count = collections.Counter(s)
    if any(count[c] < k for c in 'abc'):
      return -1

    l = 0
    for r, c in enumerate(s):
      count[c] -= 1
      while count[c] < k:
        count[s[l]] += 1
        l += 1
      ans = min(ans, n - (r - l + 1))

    return ans
# code by PROGIEZ

Additional Resources

See also  913. Cat and Mouse LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.