class Solution {
public List<String> commonChars(String[] A) {
//存放结果list
List<String> list = new LinkedList<>();
//开一个HashMap类型的数组
HashMap[] map = new HashMap[A.length];
//把输入的字符串数组的值的每个字符出现的个数都给计算出来
//存放到HashMap里面,然后将HashMap放入HashMap类型的数组里面
for (int i = 0; i < A.length; i++) {
HashMap<Character, Integer> hashMap = new HashMap<>();
for (int j = 0; j < A[i].length(); j++) {
if (!hashMap.containsKey(A[i].charAt(j))) {
hashMap.put(A[i].charAt(j), 1);
} else {
int value = hashMap.get(A[i].charAt(j));
value = value + 1;
hashMap.put(A[i].charAt(j), value);
}
}
map[i] = hashMap;
}
//使用HashSet去掉输入的字符串数组的第一个字符串当中的重复字符
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < A[0].length(); i++) {
set.add(A[0].charAt(i));
}
//得到过滤以后的字符串
String filter = "";
for (Character ch : set) {
filter = filter + ch;
}
//数组中的第一个字符串去掉重复以后进行遍历,遍历我们得到的存放每个字符和它出现的个数的Map,遍历的去拿到在所有的字符串当中都重复的元素。然后把这个字符加入到我们的结果list当中
for (int i = 0; i < filter.length(); i++) {
String str1 = String.valueOf(map[0].get(filter.charAt(i)));
int min = Integer.parseInt(str1);
boolean flag = false;
for (int j = 0; j < map.length; j++) {
if (!map[j].containsKey(filter.charAt(i))) {
flag = true;
break;
}
else {
String str = String.valueOf(map[j].get(filter.charAt(i)));
int value = Integer.parseInt(str);
min = Math.min(min, value);
flag = false;
}
}
if (!flag) {
for (int j = 0; j < min; j++) {
String s = String.valueOf(filter.charAt(i));
list.add(s);
}
}
}
return list;
}
}
04-23