Compress the String!

  • + 0 comments

    from itertools import groupby

    def run_length_encoding(s): encoded_parts = [] for key, group in groupby(s): count = len(list(group)) encoded_parts.append(f'({count}, {key})') return ' '.join(encoded_parts)

    input_string = input() output_string = run_length_encoding(input_string) print(output_string)