Better Compression π¦
Consider a string, S, that is a series of characters, each followed by its frequency as an integer. The string is not compressed correctly, so there may be multiple occurrences of the same character. A properly compressed string will consist of one instance of each character in alphabetical order followed by the total count of that character within the string.
Example
The string 'a3c9b2c1' has two instances where 'c' is followed by a count: once with 9 occurrences, and again with 1. It should be compressed to 'a3b2c10'.
Complete the function betterCompression in the editor below.
betterCompression has the following parameter:
S: string: a compressed string
Returns
string: the properly compressed string
1Example 1
'a3c9b2c1' has two instances where 'c' is followed by a count: once with 9 occurrences, and again with 1. It should be compressed to 'a3b2c10'.Constraints
Limits and guarantees your solution can rely on.
1 β€ size of S β€ 100000'a' β€ characters in S β€ 'z'1 β€ frequency of each character in S β€ 1000