Advertisement
Rosamonaar

neznayka

Aug 24th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. public class Main {
  2.     public static void main(String[] args)  {
  3.         ArrayList<String> normalWords = readWords();
  4.         ArrayList<String> suspiciousWords = readWords();
  5.  
  6.         for (String word1 : normalWords) {
  7.             int count = 0;
  8.             for (String word2 : suspiciousWords) {
  9.                 if (isHaveOneDiffLetter(word1, word2)) {
  10.                     count++;
  11.                 }
  12.             }
  13.             System.out.println(count);
  14.         }
  15.     }
  16.  
  17.     private static boolean isHaveOneDiffLetter(String word1, String word2) {
  18.         if (word1.equals(word2) || word1.length() != word2.length()) {
  19.             return false;
  20.         }
  21.         int count = 0;
  22.         for (int i = 0; i < word1.length(); i++) {
  23.             if (word1.charAt(i) != word2.charAt(i)) {
  24.                 count++;
  25.                 if (count > 1) {
  26.                     return false;
  27.                 }
  28.             }
  29.         }
  30.         return count == 1;
  31.     }
  32.  
  33.  
  34.     private static ArrayList<String> readWords() {
  35.         Scanner scanner = new Scanner(System.in);
  36.         ArrayList<String> arrayList = new ArrayList<>();
  37.         int n = scanner.nextInt();
  38.         for (int i = 0; i < n; i++) {
  39.             arrayList.add(scanner.next());
  40.         }
  41.         return arrayList;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement