题目描述
题解
-
记录B中每个字母的最大的个数
-
根据B中记录的每个字母的最大个数,统计A中的每个字符串中的字母个数是否满足,满足的话就放到res数组中
代码
class Solution {
public List<String> wordSubsets(String[] A, String[] B) {
List<String> res = new ArrayList<String>();
int[] total = new int[26];
for(String s:B){
int[] local = new int[26];
for(int i = 0; i < s.length(); i++){
local[s.charAt(i) - 'a']++;
}
for(int j = 0;j<26;j++){
if(total[j] < local[j]){
total[j] = local[j];
}
}
}
boolean flag = true;
for(String s : A){
int[] local = new int[26];
for(int i = 0;i<s.length();i++){
local[s.charAt(i) - 'a']++;
}
for(int j = 0; j < 26; j++){
if(total[j] > local[j] && total[j] != 0){
flag = false;
break;
}
}
if(flag){
res.add(s);
}else{
flag = true;
}
}
return res;
}
}