Skip to main content

#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;

}

4 comentarios
0/9000