#118 - Find Common Characters
public List<String> commonChars(List<String> strs){
//solution here
Integer[] listCount = new Integer[26];
for (Integer i = 0; i < 26; i++)
listCount[i] = 2147483647;
for (String s : strs){
Integer[] wordCount = new Integer[26];
for (Integer i = 0; i < 26; i++)
wordCount[i] = 0;
for (Integer i = 0; i < s.length(); i++){
wordCount[s.charAt(i) - 97] = wordCount[s.charAt(i) - 97] + 1;
}
System.debug(wordCount);
for (Integer i = 0; i < 26; i++)
listCount[i] = Math.min(listCount[i], wordCount[i]);
}
System.debug(listCount);
List<String> res = new List<String>();
for (Integer i = 0; i < 26; i++){
Integer count = listCount[i];
while (count > 0){
count--;
String myChar = String.fromCharArray(new List<integer>{ i + 97 });
res.add(myChar);
}
}
return res;
}
Hey @Sunil Manjunath,
So happy someone asked me question :-)))))
So I don't remember the exact problem but here goes:
Integer i = charMap.get(a)/frq;
CharMap is map containing all the characters as key, and value is how many times they are they occur. What I am doing here is, I am dividing the number by number of words provided in the original list.
Basically I am checking if a string appears atleast 1 or more times in all original words that were provided in the list, and if it appear more than 0 times i.e. 1 or greater, i get that character in my list which I am returning back through the method.
Hope this makes sense.