题目
类型:Array
解题思路
先统计 licensePlate 中每个字母的出现次数(忽略大小写),然后遍历 words 中的每个单词,若 26 个字母在该单词中的出现次数均不小于在 licensePlate 中的出现次数,则该单词是一个补全词。返回最短且最靠前的补全词。
代码
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] cnt = new int[26];
for (int i = 0; i < licensePlate.length(); ++i) {
char ch = licensePlate.charAt(i);
if (Character.isLetter(ch)) {
++cnt[Character.toLowerCase(ch) - 'a'];
}
}
int idx = -1;
for (int i = 0; i < words.length; ++i) {
int[] c = new int[26];
for (int j = 0; j < words[i].length(); ++j) {
char ch = words[i].charAt(j);
++c[ch - 'a'];
}
boolean ok = true;
for (int j = 0; j < 26; ++j) {
if (c[j] < cnt[j]) {
ok = false;
break;
}
}
if (ok && (idx < 0 || words[i].length() < words[idx].length())) {
idx = i;
}
}
return words[idx];
}
}