Given a string licensePlate and an array of strings words, find the
shortest completing word in words.
A completing word is a word that contains all the letters in
licensePlate. Ignore numbers and spaces in licensePlate, and treat letters as case insensitive. If a letter appears more than once in
licensePlate, then it must appear in the word the same number of times or more.
For example, if licensePlate`` = "aBc 12c", then it contains letters 'a',
'b' (ignoring case), and 'c' twice. Possible completing words are
"abccdef", "caaacab", and "cbca".
Return _the shortestcompleting word in _words. It is guaranteed an answer exists. If there are multiple shortest completing words, return the
first one that occurs in words.
Input: licensePlate ="1s3 PSt", words =["step","steps","stripe","stepple"]Output: "steps"Explanation: licensePlate contains letters 's','p','s'(ignoring case), and 't'."step" contains 't' and 'p', but only contains 1's'."steps" contains 't','p', and both 's' characters."stripe"is missing an 's'."stepple"is missing an 's'.Since "steps"is the only word containing all the letters, that is the answer.
Input: licensePlate ="1s3 456", words =["looks","pest","stew","show"]Output: "pest"Explanation: licensePlate only contains the letter 's'. All the words contain 's', but among these "pest","stew", and "show" are shortest. The answer is"pest" because it is the word that appears earliest of the 3.
We need to check for each word if it contains all the required letters (with counts) from the license plate. By counting the frequency of each letter in the license plate and each word, we can efficiently check if a word is a completing word.
classSolution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] cnt =newint[26];
for (char c : licensePlate.toCharArray()) {
if (Character.isLetter(c)) cnt[Character.toLowerCase(c) -'a']++;
}
String ans =null;
for (String w : words) {
int[] wc =newint[26];
for (char c : w.toCharArray()) wc[c -'a']++;
boolean ok =true;
for (int i = 0; i < 26; ++i) {
if (cnt[i]> wc[i]) { ok =false; break; }
}
if (ok && (ans ==null|| w.length() < ans.length())) ans = w;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funshortestCompletingWord(licensePlate: String, words: Array<String>): String {
val cnt = IntArray(26)
for (c in licensePlate) {
if (c.isLetter()) cnt[c.lowercaseChar() - 'a']++ }
var ans: String? = nullfor (w in words) {
val wc = IntArray(26)
for (c in w) wc[c - 'a']++var ok = truefor (i in0 until 26) {
if (cnt[i] > wc[i]) { ok = false; break }
}
if (ok && (ans ==null|| w.length < ans.length)) ans = w
}
return ans!! }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defshortestCompletingWord(self, licensePlate: str, words: list[str]) -> str:
cnt = [0] *26for c in licensePlate:
if c.isalpha():
cnt[ord(c.lower()) - ord('a')] +=1 ans =''for w in words:
wc = [0] *26for c in w:
wc[ord(c) - ord('a')] +=1if all(cnt[i] <= wc[i] for i in range(26)):
ifnot ans or len(w) < len(ans):
ans = w
return ans
⏰ Time complexity: O(L + W * S), where L = length of licensePlate, W = number of words, S = average word length. We count letters for licensePlate and for each word.
🧺 Space complexity: O(1) for the counters (since alphabet size is constant), plus O(W * S) for input storage.