• + 0 comments

    def alternate(s):

    sum = 0
    pos = {ch:[i.start() for i in re.finditer(ch, s)] for ch in set(s)}
    for j in (x for x in combinations(pos, 2) if abs(len(pos[x[0]])-len(pos[x[1]]))<=1):
        s_pos = (j[0], j[1]) if pos[j[0][0]] < pos[j[1][0]] else (j[1], j[0])
        if len(pos[s_pos[0]]) < len(pos[s_pos[1]]):
            continue
        comb = [x for x in  (chain.from_iterable((n for n in zip_longest(pos[s_pos[0]], pos[s_pos[1]], fillvalue='_')))) if x != '_']
        if comb == sorted(comb):
            sum = max(sum, len(comb))
    
    return sum