1002. Find Common Characters

#### QUESTION:

Given an array `A` of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

You may return the answer in any order.

Example 1:

``````Input: ["bella","label","roller"]
Output: ["e","l","l"]
``````

Example 2:

``````Input: ["cool","lock","cook"]
Output: ["c","o"]
``````

Note:

1. `1 <= A.length <= 100`
2. `1 <= A[i].length <= 100`
3. `A[i][j]` is a lowercase letter

#### SOLUTION:

``````class Solution {
public List<String> commonChars(String[] A) {
ArrayList<String> result = new ArrayList<>();
if(A.length==0) return result;
HashMap<Character,Integer> first = commonCharsHelper(A[0]);
for(int i = 1;i<A.length;i++){
String nTmp = A[i];
HashMap<Character,Integer> tmp = commonCharsHelper(nTmp);
Iterator<Character> iterator = first.keySet().iterator();
while (iterator.hasNext()){
Character next = iterator.next();
first.put(next,Math.min(first.get(next),tmp.getOrDefault(next,0)));
}
}

Iterator<Map.Entry<Character, Integer>> iterator = first.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<Character, Integer> next = iterator.next();
if(next.getValue()!=0){
for(int i =0;i<next.getValue();i++){
}
}
}
return result;
}

public static HashMap<Character,Integer> commonCharsHelper(String s){
HashMap<Character,Integer> result = new HashMap<>();
char[] chars = s.toCharArray();
for(int i = 0;i<chars.length;i++){
char tmp = chars[i];
result.put(tmp,result.getOrDefault(tmp,0)+1);
}
return result;
}
}
``````