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. Scanner sc = new Scanner(System.in);
  14. int n = sc.nextInt();
  15. String[] words = new String[n];
  16. for(int i=0; i<n; i++){
  17. words[i] = sc.next();
  18. }
  19. List<String> list = commonChars(words);
  20. System.out.println(list);
  21.  
  22. }
  23.  
  24. public static List<String> commonChars(String[] words){
  25. List<String> list = new ArrayList<>();
  26. char []hash = new char[26];
  27. for(char c : words[0].toCharArray()){
  28. hash[c - 'a']++;
  29. }
  30.  
  31. for(int i=1; i<words.length; i++){
  32. String str = words[i];
  33. char temp[] = new char[26];
  34. for(char c : str.toCharArray()){
  35. temp[c - 'a']++;
  36. }
  37.  
  38. for(int j=0; j<26; j++){
  39. hash[j] = (char)Math.min((int)hash[j], (int)temp[j]);
  40. temp[j] = 0;
  41. }
  42. }
  43.  
  44. for(int i=0; i<26; i++){
  45. int j = hash[i];
  46. while(j!=0){
  47. char c = (char)(i + 'a');
  48. list.add(c+"");
  49. j--;
  50. }
  51. }
  52. return list;
  53. }
  54. }
Success #stdin #stdout 0.17s 60824KB
stdin
3
bella label roller
stdout
[e, l, l]