The Indian Engineer

Problem 2516 Take K of Each Character From Left and Right

Posted on 6 mins

Hash-Table String Sliding Window

Problem Statement

Link - Problem 2516

Question

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:

Solution

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

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

        for (int i = 0; i < 3; i++) {
            if (count[i] < k)
                return -1;
        }

        vector<int> window(3, 0);
        int left = 0, max_window = 0;

        for (int right = 0; right < n; right++) {
            window[s[right] - 'a']++;

            while (left <= right && (count[0] - window[0] < k || count[1] - window[1] < k || count[2] - window[2] < k)) {
                window[s[left] - 'a']--;
                left++;
            }

            max_window = max(max_window, right - left + 1);
        }

        return n - max_window;
    }
};

Complexity Analysis

| Algorithm                        | Time Complexity | Space Complexity |
| -------------------------------- | --------------- | ---------------- |
| Sliding Window with Two Pointers | O(n)            | O(1)             |

Explanation

Intial Thoughts

My initial thoughts on this problem revolve around identifying the characters at both ends of the string. This problem asks us to find the minimum number of minutes to get at least k of each character, so we need to think about the optimal strategy to take characters from both the left and right sides. I think about this like trying to solve a puzzle where we have to balance taking characters from both sides to minimize the total time. I also consider how choosing characters from one side affects our ability to get enough characters from the other side.

Intuitive Analysis

Upon deeper analysis, I realize that my initial intuition about balancing the characters from both sides is key. By maintaining a sliding window that captures our total count of each character, we ensure we know exactly how many we have of each character. The key insight here is to understand that taking characters from the left and right sides affects the total number of characters we have. We should keep removing characters from the left until we have enough characters of each type. We need to consider the characters we cannot move to the left side without affecting our total count of characters, indicating that we cannot take less than a certain number of characters. We continuously update our maximum window count to find the minimum number of minutes needed.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Start by counting the frequency of each character and checking if it is possible.

If you take x characters from the left side, what is the minimum number of characters you need to take from the right side? Find this for all values of x in the range 0 ≤ x ≤ s.length.

Use a two-pointers approach to avoid computing the same information multiple times.


Similar Questions:

Title URL Difficulty
Merge Sorted Array https://leetcode.com/problems/merge-sorted-array Easy
Reorder List https://leetcode.com/problems/reorder-list Medium
Defuse the Bomb https://leetcode.com/problems/defuse-the-bomb Easy