Code RoomEncode run-length
MediumPrep Room Coding #574

Encode run-length

CodingAlgorithms & data structuresEntry–Mid~15 min

A log shipper shortens noisy lines before upload. Given a string of lowercase letters, build its run-length form: each maximal run of a repeated character becomes the character followed by the run's length in decimal (lengths can be multi-digit, and 1 is written explicitly). Ship whichever is better: return the encoded form only if it is strictly shorter than the original; otherwise return the original. For example, 'aaabbbbbcc' becomes 'a3b5c2', but 'ab' stays 'ab' because 'a1b1' is longer.

Implement
compress_log(s: str) → str
Examples
in["aaabbbbbcc"]out"a3b5c2"
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
["aaabbbbbcc"]"a3b5c2"not run yetsample