char_groups = groupby(inp)
counts = [(char, len(list(group))) for char, group in char_groups]
max_idx = max(list(range(len(counts))), key=lambda i: counts[i][1] if counts[i][0
    ] == 'a' else 0)
result = ''.join(char * count for char, count in counts[:max_idx]
    ) + 'aa' + ''.join(char * count for char, count in counts[max_idx + 1:])
Count the number of max consecutive "a"'s from a string. Python 3