Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- ArrayList<String> normalWords = readWords();
- ArrayList<String> suspiciousWords = readWords();
- for (String word1 : normalWords) {
- int count = 0;
- for (String word2 : suspiciousWords) {
- if (isHaveOneDiffLetter(word1, word2)) {
- count++;
- }
- }
- System.out.println(count);
- }
- }
- private static boolean isHaveOneDiffLetter(String word1, String word2) {
- if (word1.equals(word2) || word1.length() != word2.length()) {
- return false;
- }
- int count = 0;
- for (int i = 0; i < word1.length(); i++) {
- if (word1.charAt(i) != word2.charAt(i)) {
- count++;
- if (count > 1) {
- return false;
- }
- }
- }
- return count == 1;
- }
- private static ArrayList<String> readWords() {
- Scanner scanner = new Scanner(System.in);
- ArrayList<String> arrayList = new ArrayList<>();
- int n = scanner.nextInt();
- for (int i = 0; i < n; i++) {
- arrayList.add(scanner.next());
- }
- return arrayList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement