Quant Memo
Coding/●●●●●

Run-length string compression

Implement basic run-length encoding: replace each run of a repeated character with the character followed by its count. If the "compressed" string would not be strictly shorter than the original, return the original unchanged.

"aabcccccaaa" -> "a2b1c5a3"     # 8 chars vs 11
"abc"         -> "abc"          # "a1b1c1" is longer; keep original
""            -> ""

Return the compressed (or original) string in O(n)O(n) time.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions