shlogg · Early preview
Md Ariful Haque @mah-shamim

String Compression III: Compressing Strings With PHP

Compress string using greedy approach: take longest prefix of repeating chars (up to 9) and append count & char. Example: 'abcde' -> '1a1b1c1d1e', 'aaaaaaaaaaaaaabb' -> '9a5a2b

3163. String Compression III
Difficulty: Medium
Topics: String
Given a string word, compress it using the following algorithm:

Begin with an empty string comp. While word is not empty, use the following operation:

Remove a maximum length prefix of word made of a single character c repeating at most 9 times.
Append the length of the prefix followed by c to comp.



Return the string comp.
Example 1:

Input: word = "abcde"
Output: "1a1b1c1d1e"
Explanation: Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.

For each prefix,...