Simooo

Magic exchangeable words

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