FunctionRacerProblemsRace

Compress Only the Long Runs

EasyStringsPython, JavaScript, TypeScript, Java, C++

A run is a maximal stretch of equal characters in s. Compress s by replacing every run of 3 or more characters with the character followed by the length of the run written in decimal, while runs of 1 or 2 characters are copied as they are. Return the compressed string; an empty s gives "".

For example "aaabccdddd" becomes "a3bccd4": the runs aaa and dddd are long enough to compress, b and cc are not.

Example 1

s "aaabccdddd"
returns "a3bccd4"

aaa becomes a3, b and cc stay, dddd becomes d4.

Example 2

s "abc"
returns "abc"

Every run has length 1, so nothing is compressed.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Strings problems