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. String[] samples = {"tommy", "cannot", "solution"};
  13.  
  14. Map<Character, Integer> charCountMap = new HashMap<Character, Integer>();
  15. List<Character> seenSoFar = new ArrayList<Character>();
  16. String first = samples[0];
  17. for (Character c : first.toCharArray()) {
  18. if (seenSoFar.contains(c)) {
  19. continue;
  20. }
  21. else {
  22. seenSoFar.add(c);
  23. charCountMap.put(c, 1);
  24. }
  25. }
  26.  
  27. for (int i = 1; i < samples.length; i++) {
  28. seenSoFar = new ArrayList<Character>();
  29. String s = samples[i];
  30. for (Character c : s.toCharArray()) {
  31. if (seenSoFar.contains(c)) {
  32. continue;
  33. }
  34. else {
  35. seenSoFar.add(c);
  36. if (charCountMap.keySet().contains(c)) {
  37. Integer j = charCountMap.get(c);
  38. j++;
  39. charCountMap.put(c, j);
  40. }
  41. else {
  42. // character is not in every string
  43. }
  44. }
  45. }
  46. }
  47.  
  48. List<Character> commonToAll = new ArrayList<Character>();
  49. for (Character c : charCountMap.keySet()) {
  50. Integer charWasSeenCount = charCountMap.get(c);
  51. if (charWasSeenCount == samples.length) {
  52. commonToAll.add(c);
  53. }
  54. }
  55.  
  56. System.out.println("The following characters were found in every string examined:");
  57. for (Character c : commonToAll) {
  58. System.out.print(c + " ");
  59. }
  60. System.out.println("");
  61. }
  62. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
The following characters were found in every string examined:
t o