java.lang.Object
g0801_0900.s0820_short_encoding_of_words.Solution

public class Solution extends Object
820 - Short Encoding of Words.<p>Medium</p> <p>A <strong>valid encoding</strong> of an array of <code>words</code> is any reference string <code>s</code> and array of indices <code>indices</code> such that:</p> <ul> <li><code>words.length == indices.length</code></li> <li>The reference string <code>s</code> ends with the <code>'#'</code> character.</li> <li>For each index <code>indices[i]</code>, the <strong>substring</strong> of <code>s</code> starting from <code>indices[i]</code> and up to (but not including) the next <code>'#'</code> character is equal to <code>words[i]</code>.</li> </ul> <p>Given an array of <code>words</code>, return <em>the <strong>length of the shortest reference string</strong></em> <code>s</code> <em>possible of any <strong>valid encoding</strong> of</em> <code>words</code><em>.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;time&rdquo;, &ldquo;me&rdquo;, &ldquo;bell&rdquo;]</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> A valid encoding would be s = <code>&quot;time#bell#&quot; and indices = [0, 2, 5</code>].</p> <p>words[0] = &ldquo;time&rdquo;, the substring of s starting from indices[0] = 0 to the next &lsquo;#&rsquo; is underlined in &ldquo;time#bell#&rdquo;</p> <p>words[1] = &ldquo;me&rdquo;, the substring of s starting from indices[1] = 2 to the next &lsquo;#&rsquo; is underlined in &ldquo;time#bell#&rdquo;</p> <p>words[2] = &ldquo;bell&rdquo;, the substring of s starting from indices[2] = 5 to the next &lsquo;#&rsquo; is underlined in &ldquo;time#bell#&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;t&rdquo;]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> A valid encoding would be s = &ldquo;t#&rdquo; and indices = [0].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 2000</code></li> <li><code>1 <= words[i].length <= 7</code></li> <li><code>words[i]</code> consists of only lowercase letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimumLengthEncoding

      public int minimumLengthEncoding(String[] words)