String Compression
Medium
strings
Microsoft
Apple
Implement run-length encoding: compress a string by replacing consecutive repeated characters with the character followed by the count. Only compress if it would be shorter.
Example
compress("aabcccccaaa")
# => "a2b1c5a3"
compress("abc")
# => "abc" (compressed would be longer)
Test Cases
Python Editor
Output
Click "Run" to see results...