题目链接
leetcode748
class Solution {
int[] thymeleaf = new int[26];
public String shortestCompletingWord(String licensePlate, String[] words) {
String ans = "";
int minlength = 16;
licensePlate = licensePlate.toLowerCase();
for (int i = 0; i < licensePlate.length(); i++) {
if (Character.isLetter(licensePlate.charAt(i))) {
thymeleaf[licensePlate.charAt(i) - 'a']++;
}
}
for (String word : words) {
if (word.length() < minlength && match(word)) {
minlength = word.length();
ans = word;
}
}
return ans;
}
public boolean match(String word) {
int[] target = new int[26];
for (int i = 0; i < word.length(); i++) {
target[word.charAt(i) - 'a']++;
}
for (int i = 0; i < 26; i++) {
if(target[i] < thymeleaf[i]){
return false;
}
}
return true;
}
}