748. 最短完整词
代码:
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
int[] countWorld = new int[27];
String str = null;
licensePlate = licensePlate.toLowerCase();
for (int i = 0; i < licensePlate.length(); i++)
{
if (licensePlate.charAt(i) >= 'a' && licensePlate.charAt(i) <= 'z')
countWorld[licensePlate.charAt(i) - '0']++;
}
for (int i = 0; i < words.length; i++)
{
int[] count = new int[27];
words[i] = words[i].toLowerCase();
for (int j = 0; j < words[i].length(); j++)
{
if (words[i].charAt(j) >= 'a' && words[i].charAt(j) <= 'z')
count[words[i].charAt(j) - '0']++;
}
int minLength = Integer.MAX_VALUE, flag = 0;
for (int j = 0; j < 27; j++)
{
if (count[j] < countWorld[j]) {
flag = 1;
break;
}
}
if (flag == 0) {
if (minLength > words[i].length()) {
minLength = words[i].length();
str = words[i];
}
}
}
return str;
}
}