A network layer compresses each packet payload using run-length counts: every maximal run of one repeated character becomes that character followed by the run's length (for example aabb becomes a2b2). To avoid ever making a payload bigger, the layer sends the compressed form only if it is strictly shorter than the original payload; otherwise it sends the original payload unchanged.
Given a payload of lowercase English letters, print whichever of the two the layer would send.
Line 1: a non-empty string of lowercase English letters.
A single line: the run-length compressed string if it is strictly shorter than the input, otherwise the original input string.
a-z).Example 1
Input
aabcccccaaa
Expected
a2b1c5a3
Explanation
The run-length form 'a2b1c5a3' has length 8, shorter than the original length 11, so it is sent.
Example 2
Input
abcdef
Expected
abcdef
Explanation
The run-length form 'a1b1c1d1e1f1' has length 12, not shorter than 6, so the original 'abcdef' is sent.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →