Advertisement
Guest User

magic

a guest
Oct 20th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5.  
  6. namespace _05.Magic_exchangeable_words
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string[] input = Console.ReadLine().Split();
  13.             bool exchangeble =
  14.                 AreTheyExchangebles(input[0], input[1]);
  15.             if (exchangeble)
  16.             {
  17.                 Console.WriteLine("true");
  18.             }
  19.             else
  20.             {
  21.                 Console.WriteLine("false");
  22.             }
  23.            
  24.         }
  25.  
  26.         private static bool AreTheyExchangebles(string v1, string v2)
  27.         {
  28.             Dictionary<char, char> dict = new Dictionary<char, char>();
  29.             int minLength = Math.Min(v1.Length, v2.Length);
  30.             for (int i = 0; i < minLength; i++)
  31.             {
  32.                
  33.                 if (dict.ContainsKey(v1[i]))
  34.                 {
  35.                     if (dict[v1[i]] != v2[i])
  36.                     {
  37.                         return false;
  38.                     }
  39.                 }
  40.                 else
  41.                 {
  42.                     if (dict.ContainsValue(v2[i]))
  43.                     {
  44.                         return false;
  45.                     }
  46.                     dict[v1[i]] = v2[i];
  47.                 }
  48.             }
  49.             if (v1.Length > v2.Length)
  50.             {
  51.                 for (int i = minLength; i < v1.Length; i++)
  52.                 {
  53.                     if (!dict.ContainsKey(v1[i]))
  54.                     {
  55.                         return false;
  56.                     }
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 for (int i = minLength ; i < v2.Length; i++)
  62.                 {
  63.                     if (!dict.ContainsValue(v2[i]))
  64.                     {
  65.                         return false;
  66.                     }
  67.                 }
  68.             }
  69.             return true;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement