Venciity

[JAVA] Cognate Words авторско решение

Jul 23rd, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.HashSet;
  3.  
  4. public class CognateWords {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         String[] words = sc.nextLine().split("[^a-zA-Z]+");
  9.        
  10.         HashSet<String> cognateWords = new HashSet<>();
  11.         for (int a = 0; a < words.length; a++) {
  12.             for (int b = 0; b < words.length; b++) {
  13.                 for (int c = 0; c < words.length; c++) {
  14.                     // Check if a!=b and a|b=c
  15.                     if (a != b) {
  16.                         boolean check = (words[a] + words[b]).equals(words[c]);
  17.                         if (check) {
  18.                             cognateWords.add(words[a] + "|" + words[b] + "=" + words[c]);
  19.                         }
  20.                     }                  
  21.                 }
  22.             }
  23.         }
  24.        
  25.         if (cognateWords.isEmpty()) {
  26.             System.out.println("No");
  27.         } else {
  28.             for (String cognate : cognateWords) {
  29.                 System.out.println(cognate);
  30.             }
  31.         }
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment