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.  
  11. public static void main(String[] args) {
  12.  
  13. String s1 ="abcc";
  14. String s2 = "cbaa";
  15.  
  16. getCommon(s1,s2);
  17.  
  18. }
  19.  
  20. public static void getCommon(String s1,String s2){
  21.  
  22.  
  23. char[] s1Array = s1.toCharArray();
  24. char [] s2Array = s2.toCharArray();
  25.  
  26.  
  27. Set<Character>s1CharSet = new HashSet<Character>();
  28. Set<Character>s2CharSet = new HashSet<Character>();
  29.  
  30. for(char c:s1Array){
  31. s1CharSet.add(c);
  32. }
  33.  
  34. for(char c: s2Array){
  35. s2CharSet.add(c);
  36. }
  37.  
  38. s1CharSet.retainAll(s2CharSet);
  39.  
  40. if(s1CharSet.size()==0){
  41. System.out.println("There are no common characters between the two strings");
  42. }
  43.  
  44. else{
  45. System.out.println(s1CharSet);
  46. }
  47.  
  48.  
  49. }
  50.  
  51. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
[b, c, a]