Problem statement

https://binarysearch.com/problems/Removing-Triple-Successive-Duplicates/

Solution

For each group it is enough to replace len of group // 3 symbols.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        ans = 0
        for _, x in groupby(s):
            ans += len(list(x)) // 3
        return ans