java.lang.Object
g2101_2200.s2135_count_words_obtained_after_adding_a_letter.Solution

public class Solution extends Object
2135 - Count Words Obtained After Adding a Letter.<p>Medium</p> <p>You are given two <strong>0-indexed</strong> arrays of strings <code>startWords</code> and <code>targetWords</code>. Each string consists of <strong>lowercase English letters</strong> only.</p> <p>For each string in <code>targetWords</code>, check if it is possible to choose a string from <code>startWords</code> and perform a <strong>conversion operation</strong> on it to be equal to that from <code>targetWords</code>.</p> <p>The <strong>conversion operation</strong> is described in the following two steps:</p> <ol> <li><strong>Append</strong> any lowercase letter that is <strong>not present</strong> in the string to its end. <ul> <li>For example, if the string is <code>&quot;abc&quot;</code>, the letters <code>'d'</code>, <code>'e'</code>, or <code>'y'</code> can be added to it, but not <code>'a'</code>. If <code>'d'</code> is added, the resulting string will be <code>&quot;abcd&quot;</code>.</li> </ul> </li> <li><strong>Rearrange</strong> the letters of the new string in <strong>any</strong> arbitrary order. <ul> <li>For example, <code>&quot;abcd&quot;</code> can be rearranged to <code>&quot;acbd&quot;</code>, <code>&quot;bacd&quot;</code>, <code>&quot;cbda&quot;</code>, and so on. Note that it can also be rearranged to <code>&quot;abcd&quot;</code> itself.</li> </ul> </li> </ol> <p>Return <em>the <strong>number of strings</strong> in</em> <code>targetWords</code> <em>that can be obtained by performing the operations on <strong>any</strong> string of</em> <code>startWords</code>.</p> <p><strong>Note</strong> that you will only be verifying if the string in <code>targetWords</code> can be obtained from a string in <code>startWords</code> by performing the operations. The strings in <code>startWords</code> <strong>do not</strong> actually change during this process.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> startWords = [&ldquo;ant&rdquo;,&ldquo;act&rdquo;,&ldquo;tack&rdquo;], targetWords = [&ldquo;tack&rdquo;,&ldquo;act&rdquo;,&ldquo;acti&rdquo;]</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>In order to form targetWords[0] = &ldquo;tack&rdquo;, we use startWords[1] = &ldquo;act&rdquo;, append &lsquo;k&rsquo; to it, and rearrange &ldquo;actk&rdquo; to &ldquo;tack&rdquo;.</p> </li> <li> <p>There is no string in startWords that can be used to obtain targetWords[1] = &ldquo;act&rdquo;.</p> </li> </ul> <p>Note that &ldquo;act&rdquo; does exist in startWords, but we <strong>must</strong> append one letter to the string before rearranging it.</p> <ul> <li>In order to form targetWords[2] = &ldquo;acti&rdquo;, we use startWords[1] = &ldquo;act&rdquo;, append &lsquo;i&rsquo; to it, and rearrange &ldquo;acti&rdquo; to &ldquo;acti&rdquo; itself.</li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> startWords = [&ldquo;ab&rdquo;,&ldquo;a&rdquo;], targetWords = [&ldquo;abc&rdquo;,&ldquo;abcd&rdquo;]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>In order to form targetWords[0] = &ldquo;abc&rdquo;, we use startWords[0] = &ldquo;ab&rdquo;, add &lsquo;c&rsquo; to it, and rearrange it to &ldquo;abc&rdquo;.</p> </li> <li> <p>There is no string in startWords that can be used to obtain targetWords[1] = &ldquo;abcd&rdquo;.</p> </li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= startWords.length, targetWords.length <= 5 * 10<sup>4</sup></code></li> <li><code>1 <= startWords[i].length, targetWords[j].length <= 26</code></li> <li>Each string of <code>startWords</code> and <code>targetWords</code> consists of lowercase English letters only.</li> <li>No letter occurs more than once in any string of <code>startWords</code> or <code>targetWords</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • wordCount

      public int wordCount(String[] startWords, String[] targetWords)