Take k of each character from left and right

December 25, 2022

array-and-hashmap two-pointers

Problem URL: Take k of each character from left and right

We will count the occurance of a, b and c. Then we will iterate over the string and take k characters from the left and right. We will update the result.

class Solution:
    def takeCharacters(self, s: str, k: int) -> int:
        ra = s.count('a') - k
        rb = s.count('b') - k
        rc = s.count('c') - k

        if any(i < 0 for i in [ra, rb, rc]):
            return -1

        count = collections.defaultdict(int)
        r = l = res = 0

        for ch in s:
            count[ch] += 1
            r += 1

            while count['a'] > ra or count['b'] > rb or count['c'] > rc:
                count[s[l]] -= 1
                r -= 1
                l += 1

            res = max(res, r)

        return len(s) - res

Time complexity: O(n)
Space complexity: O(1)