fork download
  1. import java.util.HashSet;
  2. import java.util.Scanner;
  3. import java.util.Set;
  4.  
  5. /**
  6.  * Created by studyalgorithms.com
  7.  */
  8.  
  9. class TwoStrings {
  10.  
  11. public static void main(String[] args) {
  12. Scanner scan = new Scanner(System.in);
  13. int n = scan.nextInt();
  14.  
  15. Set<Character> a;
  16. Set<Character> b;
  17.  
  18. for (int i = 0; i < n; i++) {
  19.  
  20. a = new HashSet<>();
  21. b = new HashSet<>();
  22.  
  23. for (char c : scan.next().toCharArray()) {
  24. a.add(c);
  25. }
  26. for (char c : scan.next().toCharArray()) {
  27. b.add(c);
  28. }
  29.  
  30. // store the set intersection in set 'a'
  31. a.retainAll(b);
  32.  
  33. System.out.println((a.isEmpty()) ? "NO" : "YES");
  34. }
  35. scan.close();
  36. }
  37. }
Success #stdin #stdout 0.05s 4386816KB
stdin
2
hello
world
hi
world
stdout
YES
NO