Problem statement

https://binarysearch.com/problems/Pair-and-Triples/

Solution

I think I saw this on leetcode. Use counter and check that we have frequencies divisible by 3 for all elements except one and have reminder 2 for one element.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, s):
        cnt = list(Counter(s).values())
        if any(x % 3 == 1 for x in cnt): return False
        if sum(x % 3 == 2 for x in cnt) != 1: return False
        return True