Two Characters

  • + 0 comments

    Python3:

    def alternate(s):
        mem = Counter(s)
        cb = filter(lambda p: abs(mem[p[0]] - mem[p[1]]) <= 1, combinations(mem.keys(), 2))
        cb = sorted(cb, key= lambda p: mem[p[0]] + mem[p[1]], reverse= True)
        
        for c1, c2 in cb:
            ss = [c for c in s if c in (c1, c2)]
            if all(ss[i] != ss[i+1] for i in range(len(ss) - 1)):
                return len(ss)
        return 0