Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
Example 1:
Input: words = ["bella","label","roller"] Output: ["e","l","l"]
Example 2:
Input: words = ["cool","lock","cook"] Output: ["c","o"]
Constraints:
1 <= words.length <= 1001 <= words[i].length <= 100words[i] consists of lowercase English letters.class Solution {
int[] commonAlpha = new int[26];
private void isCommon(String s){
char[] S = s.toCharArray();
int[] freq = new int[26];
for(char c: S)
freq[c-'a']++;
for(int i=0; i<26; i++)
commonAlpha[i] = Math.min(commonAlpha[i], freq[i]);
}
public List<String> commonChars(String[] words) {
for(int i=0; i<26; i++)
commonAlpha[i] = Integer.MAX_VALUE;
for(String s: words)
isCommon(s);
List<String> ans = new ArrayList<>();
for(int i=0; i<26; i++)
while(commonAlpha[i]-->0)
ans.add(String.valueOf((char)(i+'a')));
return ans;
}
}