Problem · Medium · 20 min
Shorter text
- Medium
- Strings
A run is a group of the same character that stand side by side. In "aaaabbbcc" there are three runs: aaaa, bbb and cc.
Write compress(text). It writes each run as the character and the length of the run. "aaaabbbcc" becomes "a4b3c2".
- If the result is not shorter than the original text, return the original text.
"aabb"would become"a2b2", which has the same length, so return"aabb". - The text has no digits. It can be empty.
Examples
| Call | Result |
|---|---|
compress('aaaabbbcc') |
'a4b3c2' |
compress('aabb') |
'aabb' |
compress('') |
'' |
Tasks
2 to do
Task 1, not checked yet. Make
compresspass the example tests.Task 2, not checked yet. Make
compresspass the hidden tests.
Task 1 of 2
Make compress pass the example tests.