Class Solution

java.lang.Object
g1001_1100.s1048_longest_string_chain.Solution

public class Solution extends Object
1048 - Longest String Chain.<p>Medium</p> <p>You are given an array of <code>words</code> where each word consists of lowercase English letters.</p> <p><code>word<sub>A</sub></code> is a <strong>predecessor</strong> of <code>word<sub>B</sub></code> if and only if we can insert <strong>exactly one</strong> letter anywhere in <code>word<sub>A</sub></code> <strong>without changing the order of the other characters</strong> to make it equal to <code>word<sub>B</sub></code>.</p> <ul> <li>For example, <code>&quot;abc&quot;</code> is a <strong>predecessor</strong> of <code>&quot;abac&quot;</code>, while <code>&quot;cba&quot;</code> is not a <strong>predecessor</strong> of <code>&quot;bcad&quot;</code>.</li> </ul> <p>A <strong>word chain</strong> is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, &hellip;, word<sub>k</sub>]</code> with <code>k >= 1</code>, where <code>word<sub>1</sub></code> is a <strong>predecessor</strong> of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a <strong>predecessor</strong> of <code>word<sub>3</sub></code>, and so on. A single word is trivially a <strong>word chain</strong> with <code>k == 1</code>.</p> <p>Return <em>the <strong>length</strong> of the <strong>longest possible word chain</strong> with words chosen from the given list of</em> <code>words</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> words = [&ldquo;a&rdquo;,&ldquo;b&rdquo;,&ldquo;ba&rdquo;,&ldquo;bca&rdquo;,&ldquo;bda&rdquo;,&ldquo;bdca&rdquo;]</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> One of the longest word chains is [&ldquo;a&rdquo;,&ldquo;ba&rdquo;,&ldquo;bda&rdquo;,&ldquo;bdca&rdquo;].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> words = [&ldquo;xbc&rdquo;,&ldquo;pcxbcf&rdquo;,&ldquo;xb&rdquo;,&ldquo;cxbc&rdquo;,&ldquo;pcxbc&rdquo;]</p> <p><strong>Output:</strong> 5</p> <p><strong>Explanation:</strong> All the words can be put in a word chain [&ldquo;xb&rdquo;, &ldquo;xbc&rdquo;, &ldquo;cxbc&rdquo;, &ldquo;pcxbc&rdquo;, &ldquo;pcxbcf&rdquo;].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> words = [&ldquo;abcd&rdquo;,&ldquo;dbqca&rdquo;]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong> The trivial word chain [&ldquo;abcd&rdquo;] is one of the longest word chains. [&ldquo;abcd&rdquo;,&ldquo;dbqca&rdquo;] is not a valid word chain because the ordering of the letters is changed.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= words.length <= 1000</code></li> <li><code>1 <= words[i].length <= 16</code></li> <li><code>words[i]</code> only consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details