##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/748.html

# 748. Shortest Completing Word (Easy)

Find the minimum length word from a given dictionary words, which has all the letters from the string licensePlate. Such a word is said to complete the given string licensePlate

Here, for letters we ignore case. For example, "P" on the licensePlate still matches "p" on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given a licensePlate of "PP", the word "pair" does not complete the licensePlate, but the word "supper" does.

Example 1:

Input: licensePlate = "1s3 PSt", words = ["step", "steps", "stripe", "stepple"]
Output: "steps"
Explanation: The smallest length word that contains the letters "S", "P", "S", and "T".
Note that the answer is not "step", because the letter "s" must occur in the word twice.
Also note that we ignored case for the purposes of comparing whether a letter exists in the word.


Example 2:

Input: licensePlate = "1s3 456", words = ["looks", "pest", "stew", "show"]
Output: "pest"
Explanation: There are 3 smallest length words that contains the letters "s".
We return the one that occurred first.


Note:

1. licensePlate will be a string with length in range [1, 7].
2. licensePlate will contain digits, spaces, or letters (uppercase or lowercase).
3. words will have a length in the range [10, 1000].
4. Every words[i] will consist of lowercase letters, and have length in range [1, 15].

Companies:

Related Topics:
Hash Table

## Solution 1.

• class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
for (char c : licenseArray) {
if (Character.isLetter(c))
}
int minIndex = -1;
int length = words.length;
for (int i = 0; i < length; i++) {
if (minIndex < 0 || words[i].length() < words[minIndex].length())
minIndex = i;
}
}
return minIndex < 0 ? "" : words[minIndex];
}

public boolean isComplete(int[] licenseCount, String word) {
int[] wordCount = new int;
char[] wordArray = word.toCharArray();
for (char c : wordArray)
wordCount[c - 'a']++;
for (int i = 0; i < 26; i++) {
return false;
}
return true;
}
}

############

class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
String ans = null;
int n = 16;
for (String word : words) {
if (n <= word.length()) {
continue;
}
int[] t = count(word);
if (check(counter, t)) {
n = word.length();
ans = word;
}
}
return ans;
}

private int[] count(String word) {
int[] counter = new int;
for (char c : word.toCharArray()) {
if (Character.isLetter(c)) {
++counter[c - 'a'];
}
}
return counter;
}

private boolean check(int[] counter1, int[] counter2) {
for (int i = 0; i < 26; ++i) {
if (counter1[i] > counter2[i]) {
return false;
}
}
return true;
}
}

• // OJ: https://leetcode.com/problems/shortest-completing-word/
// Time: O(N)
// Space: O(1)
class Solution {
private:
vector<int> count(string &s) {
vector<int> cnt(26, 0);
for (char c : s) {
if (isalpha(c)) {
cnt[tolower(c) - 'a']++;
}
}
return cnt;
}
bool isComplete(string &s, vector<int> target) {
auto cnt = count(s);
for (int i = 0; i < 26; ++i) {
if (cnt[i] < target[i]) return false;
}
return true;
}
public:
string shortestCompletingWord(string licensePlate, vector<string>& words) {
string ans;
for (string &w : words) {
if (!isComplete(w, cnt)) continue;
if (ans.empty() || w.size() < ans.size()) ans = w;
}
return ans;
}
};

• class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
def count(word):
counter =  * 26
for c in word:
counter[ord(c) - ord('a')] += 1
return counter

def check(counter1, counter2):
for i in range(26):
if counter1[i] > counter2[i]:
return False
return True

counter = count(c.lower() for c in licensePlate if c.isalpha())
ans, n = None, 16
for word in words:
if n <= len(word):
continue
t = count(word)
if check(counter, t):
n = len(word)
ans = word
return ans

############

import re
from collections import Counter
class Solution(object):
"""
:type words: List[str]
:rtype: str
"""
regex = re.compile('[^a-zA-Z]')
res = ''
for word in words:
if self.count(counter1, word):
if res == '' or len(word) < len(res):
res = word
return res

def count(self, counter1, word):
counter2 = Counter(word)
counter2.subtract(counter1)
return all([c >= 0 for c in counter2.values()])

• func shortestCompletingWord(licensePlate string, words []string) string {
count := func(word string) []int {
counter := make([]int, 26)
for _, c := range word {
if unicode.IsLetter(c) {
counter[c-'a']++
}
}
return counter
}

check := func(cnt1, cnt2 []int) bool {
for i := 0; i < 26; i++ {
if cnt1[i] > cnt2[i] {
return false
}
}
return true
}

var ans string
n := 16
for _, word := range words {
if n <= len(word) {
continue
}
t := count(word)
if check(counter, t) {
n = len(word)
ans = word
}
}
return ans
}