Class Solution

java.lang.Object
g0401_0500.s0443_string_compression.Solution

public class Solution extends Object
443 - String Compression.<p>Medium</p> <p>Given an array of characters <code>chars</code>, compress it using the following algorithm:</p> <p>Begin with an empty string <code>s</code>. For each group of <strong>consecutive repeating characters</strong> in <code>chars</code>:</p> <ul> <li>If the group&rsquo;s length is <code>1</code>, append the character to <code>s</code>.</li> <li>Otherwise, append the character followed by the group&rsquo;s length.</li> </ul> <p>The compressed string <code>s</code> <strong>should not be returned separately</strong> , but instead, be stored **in the input character array <code>chars</code> **. Note that group lengths that are <code>10</code> or longer will be split into multiple characters in <code>chars</code>.</p> <p>After you are done <strong>modifying the input array</strong> , return <em>the new length of the array</em>.</p> <p>You must write an algorithm that uses only constant extra space.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> chars = [&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;,&ldquo;c&rdquo;]</p> <p><strong>Output:</strong> Return 6, and the first 6 characters of the input array should be: [&ldquo;a&rdquo;,&ldquo;2&rdquo;,&ldquo;b&rdquo;,&ldquo;2&rdquo;,&ldquo;c&rdquo;,&ldquo;3&rdquo;]</p> <p><strong>Explanation:</strong> The groups are &ldquo;aa&rdquo;, &ldquo;bb&rdquo;, and &ldquo;ccc&rdquo;. This compresses to &ldquo;a2b2c3&rdquo;.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> chars = [&ldquo;a&rdquo;]</p> <p><strong>Output:</strong> Return 1, and the first character of the input array should be: [&ldquo;a&rdquo;]</p> <p><strong>Explanation:</strong> The only group is &ldquo;a&rdquo;, which remains uncompressed since it&rsquo;s a single character.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> chars = [&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;]</p> <p><strong>Output:</strong> Return 4, and the first 4 characters of the input array should be: [&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;1&rdquo;,&ldquo;2&rdquo;].</p> <p><strong>Explanation:</strong> The groups are &ldquo;a&rdquo; and &ldquo;bbbbbbbbbbbb&rdquo;. This compresses to &ldquo;ab12&rdquo;.</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> chars = [&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;b&rdquo;,&ldquo;a&rdquo;,&ldquo;a&rdquo;]</p> <p><strong>Output:</strong> Return 6, and the first 6 characters of the input array should be: [&ldquo;a&rdquo;,&ldquo;3&rdquo;,&ldquo;b&rdquo;,&ldquo;2&rdquo;,&ldquo;a&rdquo;,&ldquo;2&rdquo;].</p> <p><strong>Explanation:</strong> The groups are &ldquo;aaa&rdquo;, &ldquo;bb&rdquo;, and &ldquo;aa&rdquo;. This compresses to &ldquo;a3b2a2&rdquo;. Note that each group is independent even if two groups have the same character.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= chars.length <= 2000</code></li> <li><code>chars[i]</code> is a lowercase English letter, uppercase English letter, digit, or symbol.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • compress

      public int compress(char[] chars)