java.lang.Object
g0701_0800.s0748_shortest_completing_word.Solution

public class Solution extends Object
748 - Shortest Completing Word.<p>Easy</p> <p>Given a string <code>licensePlate</code> and an array of strings <code>words</code>, find the <strong>shortest completing</strong> word in <code>words</code>.</p> <p>A <strong>completing</strong> word is a word that <strong>contains all the letters</strong> in <code>licensePlate</code>. <strong>Ignore numbers and spaces</strong> in <code>licensePlate</code>, and treat letters as <strong>case insensitive</strong>. If a letter appears more than once in <code>licensePlate</code>, then it must appear in the word the same number of times or more.</p> <p>For example, if <code>licensePlate</code> <code>= &quot;aBc 12c&quot;</code>, then it contains letters <code>'a'</code>, <code>'b'</code> (ignoring case), and <code>'c'</code> twice. Possible <strong>completing</strong> words are <code>&quot;abccdef&quot;</code>, <code>&quot;caaacab&quot;</code>, and <code>&quot;cbca&quot;</code>.</p> <p>Return <em>the shortest <strong>completing</strong> word in</em> <code>words</code><em>.</em> It is guaranteed an answer exists. If there are multiple shortest <strong>completing</strong> words, return the <strong>first</strong> one that occurs in <code>words</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> licensePlate = &ldquo;1s3 PSt&rdquo;, words = [&ldquo;step&rdquo;,&ldquo;steps&rdquo;,&ldquo;stripe&rdquo;,&ldquo;stepple&rdquo;]</p> <p><strong>Output:</strong> &ldquo;steps&rdquo;</p> <p><strong>Explanation:</strong> licensePlate contains letters &lsquo;s&rsquo;, &lsquo;p&rsquo;, &lsquo;s&rsquo; (ignoring case), and &lsquo;t&rsquo;.</p> <p>&ldquo;step&rdquo; contains &lsquo;t&rsquo; and &lsquo;p&rsquo;, but only contains 1 &lsquo;s&rsquo;.</p> <p>&ldquo;steps&rdquo; contains &lsquo;t&rsquo;, &lsquo;p&rsquo;, and both &lsquo;s&rsquo; characters.</p> <p>&ldquo;stripe&rdquo; is missing an &lsquo;s&rsquo;.</p> <p>&ldquo;stepple&rdquo; is missing an &lsquo;s&rsquo;.</p> <p>Since &ldquo;steps&rdquo; is the only word containing all the letters, that is the answer.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> licensePlate = &ldquo;1s3 456&rdquo;, words = [&ldquo;looks&rdquo;,&ldquo;pest&rdquo;,&ldquo;stew&rdquo;,&ldquo;show&rdquo;]</p> <p><strong>Output:</strong> &ldquo;pest&rdquo;</p> <p><strong>Explanation:</strong> licensePlate only contains the letter &lsquo;s&rsquo;. All the words contain &lsquo;s&rsquo;, but among these &ldquo;pest&rdquo;, &ldquo;stew&rdquo;, and &ldquo;show&rdquo; are shortest. The answer is &ldquo;pest&rdquo; because it is the word that appears earliest of the 3.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= licensePlate.length <= 7</code></li> <li><code>licensePlate</code> contains digits, letters (uppercase or lowercase), or space <code>' '</code>.</li> <li><code>1 <= words.length <= 1000</code></li> <li><code>1 <= words[i].length <= 15</code></li> <li><code>words[i]</code> consists of lower case English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • shortestCompletingWord

      public String shortestCompletingWord(String licensePlate, String[] words)