Guest User

Untitled

a guest
Feb 18th, 2017
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. //05. Magic exchangeable words
  7. namespace MagicExchangeableWords
  8. {
  9.     class MagicExchangeableWords
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Dictionary<char, char> dict = new Dictionary<char, char>();
  14.  
  15.             string[] input = Console.ReadLine().Split();
  16.             string one = input[0];
  17.             string two = input[1];
  18.  
  19.             int minL = Math.Min(input[0].Length, input[1].Length);
  20.             bool isExchangeable = true;
  21.  
  22.             for (int i = 0; i < minL; i++)
  23.             {
  24.                 if (!dict.ContainsKey(one[i]))
  25.                 {
  26.                     if (!dict.ContainsValue(two[i]))
  27.                     {
  28.                         dict.Add(one[i], two[i]);
  29.                     }
  30.                     else
  31.                     {
  32.                         isExchangeable = false;
  33.                         break;
  34.                     }
  35.  
  36.                 }
  37.                 else
  38.                 {
  39.                     if (dict[one[i]] != two[i])
  40.                     {
  41.                         isExchangeable = false;
  42.                         break;
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             string rest = "";
  48.  
  49.             if (one.Length > two.Length)
  50.             {
  51.                 rest = one.Substring(minL);
  52.             }
  53.             else
  54.             {
  55.                 rest = two.Substring(minL);
  56.             }
  57.  
  58.             foreach (char chara in rest)
  59.             {
  60.                 if (!dict.ContainsValue(chara) && !dict.ContainsKey(chara))
  61.                 {
  62.                     isExchangeable = false;
  63.                 }
  64.             }
  65.             Console.WriteLine(isExchangeable.ToString().ToLower());
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment