
At Dropbox, you are asked to implement a lightweight string compressor using run-length encoding. Given a string s, compress consecutive repeated characters so that each group becomes the character followed by its count, then return whichever is shorter: the compressed string or the original string.
scharacter + countExample 1
s = "aaabb""a3b2"aaa and bb, so the compressed form is a3b2, which is shorter than aaabb.Example 2
s = "abc""abc"a1b1c1, which is longer than the original string.0 <= len(s) <= 10^5s may contain uppercase letters, lowercase letters, digits, spaces, or symbolss = "aaabb"Output"a3b2"WhyThere are two runs: `aaa` and `bb`. The compressed result `a3b2` has length 4, which is shorter than the original length 5.s = "abc"Output"abc"WhyThe compressed form would be `a1b1c1`, which is longer than `abc`, so the original string is returned.s = "aabcccccaaa"Output"a2b1c5a3"WhyThe runs are `aa`, `b`, `ccccc`, and `aaa`. The compressed string is shorter than the original.0 <= len(s) <= 10^5s may contain any printable charactersCompression uses consecutive character runs onlyReturn the original string if the compressed version is not strictly shorterdef compress_string(s):