Advertisement
Guest User

MagicWords

a guest
Oct 15th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1.  
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5.  
  6. public class MagicWords {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         String string1 = scan.nextLine();
  11.         String string2 = scan.nextLine();
  12.         boolean result = exchange(string1, string2);
  13.         System.out.println(result);
  14.     }
  15.  
  16.     public static boolean exchange(String string1, String string2) {
  17.         boolean result = false;
  18.         int count = 0;
  19.         HashMap<Character, Character> dictionary = new HashMap<>();
  20.  
  21.         if (string1.length() != string2.length()) {
  22.             System.out.println("Incorrect format!");
  23.             return false;
  24.         } else {
  25.             for (int i = 0; i < string1.length(); i++) {
  26.                 if (!dictionary.containsKey(string1.charAt(i))) {
  27.                     if (!dictionary.containsValue(string2.charAt(i))) {
  28.                         dictionary.put(string1.charAt(i), string2.charAt(i));
  29.                     }
  30.                 }
  31.             }
  32.             for (int i = 0; i < string1.length(); i++) {
  33.                 if (dictionary.containsKey(string1.charAt(i))) {
  34.                     if (dictionary.get(string1.charAt(i)) != string2.charAt(i)) {
  35.                         result = false;
  36.                         return result;
  37.                     } else {
  38.                         result = true;
  39.                     }
  40.                 } else {
  41.                     result = false;
  42.                     return result;
  43.                 }
  44.             }
  45.         }
  46.         return result;
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement