Better Compression
EasyParsing · Atlassian
A compressed string is a sequence of groups, where each group is a lowercase letter followed by a positive integer count, for example "a3c9b2c1". The same letter may appear in several groups, and the groups can be in any order.
Return the "better" compressed form: each letter that appears exactly once, followed by the total of all its counts, with letters in alphabetical order. For "a3c9b2c1" that is "a3b2c10".
If text is empty, return an empty string.
Example 1
- Input:
- text = "c2a10b3c1a5"
- Output:
- "a15b3c3"
- Explanation
ahas counts 10 and 5 (15),bhas 3, andchas 2 and 1 (3). Written in alphabetical order that gives"a15b3c3".
Example 2
- Input:
- text = "z1y20x300"
- Output:
- "x300y20z1"
- Explanation
- Each letter appears once, so the counts stay the same; only the order changes, to
x,y,z. The multi-digit counts 20 and 300 must be read whole.
Example 3
- Input:
- text = "q7"
- Output:
- "q7"
- Explanation
- A single group is already in its better form.
Constraints
0 <= text.length <= 10^5textis a valid sequence of groups: a lowercase letter followed by a count- Each count is an integer from
1to10^6with no leading zeros