|
| 1 | +<p>Given a string <code>word</code>, compress it using the following algorithm:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>Begin with an empty string <code>comp</code>. While <code>word</code> is <strong>not</strong> empty, use the following operation: |
| 5 | + |
| 6 | + <ul> |
| 7 | + <li>Remove a maximum length prefix of <code>word</code> made of a <em>single character</em> <code>c</code> repeating <strong>at most</strong> 9 times.</li> |
| 8 | + <li>Append the length of the prefix followed by <code>c</code> to <code>comp</code>.</li> |
| 9 | + </ul> |
| 10 | + </li> |
| 11 | +</ul> |
| 12 | + |
| 13 | +<p>Return the string <code>comp</code>.</p> |
| 14 | + |
| 15 | +<p> </p> |
| 16 | +<p><strong class="example">Example 1:</strong></p> |
| 17 | + |
| 18 | +<div class="example-block"> |
| 19 | +<p><strong>Input:</strong> <span class="example-io">word = "abcde"</span></p> |
| 20 | + |
| 21 | +<p><strong>Output:</strong> <span class="example-io">"1a1b1c1d1e"</span></p> |
| 22 | + |
| 23 | +<p><strong>Explanation:</strong></p> |
| 24 | + |
| 25 | +<p>Initially, <code>comp = ""</code>. Apply the operation 5 times, choosing <code>"a"</code>, <code>"b"</code>, <code>"c"</code>, <code>"d"</code>, and <code>"e"</code> as the prefix in each operation.</p> |
| 26 | + |
| 27 | +<p>For each prefix, append <code>"1"</code> followed by the character to <code>comp</code>.</p> |
| 28 | +</div> |
| 29 | + |
| 30 | +<p><strong class="example">Example 2:</strong></p> |
| 31 | + |
| 32 | +<div class="example-block"> |
| 33 | +<p><strong>Input:</strong> <span class="example-io">word = "aaaaaaaaaaaaaabb"</span></p> |
| 34 | + |
| 35 | +<p><strong>Output:</strong> <span class="example-io">"9a5a2b"</span></p> |
| 36 | + |
| 37 | +<p><strong>Explanation:</strong></p> |
| 38 | + |
| 39 | +<p>Initially, <code>comp = ""</code>. Apply the operation 3 times, choosing <code>"aaaaaaaaa"</code>, <code>"aaaaa"</code>, and <code>"bb"</code> as the prefix in each operation.</p> |
| 40 | + |
| 41 | +<ul> |
| 42 | + <li>For prefix <code>"aaaaaaaaa"</code>, append <code>"9"</code> followed by <code>"a"</code> to <code>comp</code>.</li> |
| 43 | + <li>For prefix <code>"aaaaa"</code>, append <code>"5"</code> followed by <code>"a"</code> to <code>comp</code>.</li> |
| 44 | + <li>For prefix <code>"bb"</code>, append <code>"2"</code> followed by <code>"b"</code> to <code>comp</code>.</li> |
| 45 | +</ul> |
| 46 | +</div> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>1 <= word.length <= 2 * 10<sup>5</sup></code></li> |
| 53 | + <li><code>word</code> consists only of lowercase English letters.</li> |
| 54 | +</ul> |
0 commit comments