fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. String[] words = {
  14. "aabbcc",
  15. "abbbcc",
  16. "abcccc"
  17. };
  18. System.out.println("common characters are " + findCommonChars(words));
  19. }
  20. static List<String> findCommonChars(String[]words){
  21. int[] count = new int[26];
  22. count = findCount(words[0]);
  23. for(int i=1;i<words.length;i++){
  24. int[] temp = findCount(words[i]);
  25. for(int j =0;j<26;j++){
  26. count[j] = Math.min(count[j],temp[j]);
  27. }
  28. }
  29. List<String> res = new ArrayList<>();
  30. for(int i=0;i<count.length;i++){
  31. if(count[i]>0){
  32. int cnt = count[i];
  33. while(cnt>0){
  34. res.add(String.valueOf((char)('a'+i)));
  35. cnt--;
  36. }
  37. }
  38. }
  39. return res;
  40. }
  41. static int[] findCount(String s){
  42. int[] res = new int[26];
  43. for(char c: s.toCharArray()){
  44. res[c-'a']++;
  45. }
  46. return res;
  47. }
  48. }
Success #stdin #stdout 0.17s 57404KB
stdin
Standard input is empty
stdout
common characters are [a, b, c, c]