java.lang.Object
g2701_2800.s2746_decremental_string_concatenation.Solution

public class Solution extends Object
2746 - Decremental String Concatenation.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>words</code> containing <code>n</code> strings.</p> <p>Let&rsquo;s define a <strong>join</strong> operation <code>join(x, y)</code> between two strings <code>x</code> and <code>y</code> as concatenating them into <code>xy</code>. However, if the last character of <code>x</code> is equal to the first character of <code>y</code>, one of them is <strong>deleted</strong>.</p> <p>For example <code>join(&quot;ab&quot;, &quot;ba&quot;) = &quot;aba&quot;</code> and <code>join(&quot;ab&quot;, &quot;cde&quot;) = &quot;abcde&quot;</code>.</p> <p>You are to perform <code>n - 1</code> <strong>join</strong> operations. Let <code>str<sub>0</sub> = words[0]</code>. Starting from <code>i = 1</code> up to <code>i = n - 1</code>, for the <code>i<sup>th</sup></code> operation, you can do one of the following:</p> <ul> <li>Make <code>str<sub>i</sub> = join(str<sub>i - 1</sub>, words[i])</code></li> <li>Make <code>str<sub>i</sub> = join(words[i], str<sub>i - 1</sub>)</code></li> </ul> <p>Your task is to <strong>minimize</strong> the length of <code>str<sub>n - 1</sub></code>.</p> <p>Return <em>an integer denoting the minimum possible length of</em> <code>str<sub>n - 1</sub></code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;aa&rdquo;,&ldquo;ab&rdquo;,&ldquo;bc&rdquo;]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:</p> <p>str<sub>0</sub> = &ldquo;aa&rdquo;</p> <p>str<sub>1</sub> = join(str<sub>0</sub>, &ldquo;ab&rdquo;) = &ldquo;aab&rdquo;</p> <p>str<sub>2</sub> = join(str<sub>1</sub>, &ldquo;bc&rdquo;) = &ldquo;aabc&rdquo;</p> <p>It can be shown that the minimum possible length of str<sub>2</sub> is 4.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;ab&rdquo;,&ldquo;b&rdquo;]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> In this example, str<sub>0</sub> = &ldquo;ab&rdquo;, there are two ways to get str<sub>1</sub>: join(str<sub>0</sub>, &ldquo;b&rdquo;) = &ldquo;ab&rdquo; or join(&ldquo;b&rdquo;, str<sub>0</sub>) = &ldquo;bab&rdquo;. The first string, &ldquo;ab&rdquo;, has the minimum length. Hence, the answer is 2.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> words = [&ldquo;aaa&rdquo;,&ldquo;c&rdquo;,&ldquo;aba&rdquo;]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> In this example, we can perform join operations in the following order to minimize the length of str<sub>2</sub>:</p> <p>str<sub>0</sub> = &ldquo;aaa&rdquo;</p> <p>str<sub>1</sub> = join(str<sub>0</sub>, &ldquo;c&rdquo;) = &ldquo;aaac&rdquo;</p> <p>str<sub>2</sub> = join(&ldquo;aba&rdquo;, str<sub>1</sub>) = &ldquo;abaaac&rdquo;</p> <p>It can be shown that the minimum possible length of str<sub>2</sub> is 6.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 1000</code></li> <li><code>1 <= words[i].length <= 50</code></li> <li>Each character in <code>words[i]</code> is an English lowercase letter</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minimizeConcatenatedLength

      public int minimizeConcatenatedLength(String[] words)