String Compression
Medium · strings
A message is stored as a string s of lowercase letters. To save space, you want to write each run of the same letter as the letter followed by the length of the run. A run is a maximal block of equal letters that sit next to each other.
If a run has length 1, write just the letter and leave the length out. Lengths can have several digits, and they are written in ordinary decimal.
Return the compressed string. For example, "aabccc" has the runs aa, b and ccc, so it becomes "a2bc3".
Examples
Example 1
Input: s = "aabccc"
Output: "a2bc3"
The runs are aa, b and ccc. The single b is written without a length.
Example 2
Input: s = "abc"
Output: "abc"
Every run has length 1, so nothing changes.
Example 3
Input: s = "zzzzzzzzzzzzq"
Output: "z12q"
There are twelve z letters in a row, so the length is written as 12.
Constraints
- 1 <= len(s) <= 10^5
- s contains only lowercase English letters