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 List<String> commonChars(String[] words) {
  11. int[] minFreq = new int[26];
  12. Arrays.fill(minFreq, Integer.MAX_VALUE);
  13.  
  14. for (String word : words) {
  15. int[] freq = new int[26];
  16. for (char c : word.toCharArray()) {
  17. freq[c - 'a']++;
  18. }
  19.  
  20. for (int i = 0; i < 26; i++) {
  21. minFreq[i] = Math.min(minFreq[i], freq[i]);
  22. }
  23. }
  24.  
  25. List<String> result = new ArrayList<>();
  26. for (int i = 0; i < 26; i++) {
  27. for (int j = 0; j < minFreq[i]; j++) {
  28. result.add(String.valueOf((char) (i + 'a')));
  29. }
  30. }
  31.  
  32. return result;
  33. }
  34.  
  35. public static void main(String[] args) {
  36. String[] words = {"bella", "label", "roller"};
  37. System.out.println("Common characters: " + commonChars(words)); // Output: [e, l, l]
  38. }
  39. }
  40.  
  41.  
  42.  
Success #stdin #stdout 0.11s 55504KB
stdin
Standard input is empty
stdout
Common characters: [e, l, l]