Advertisement
Guest User

Untitled

a guest
Feb 18th, 2017
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.65 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.  
  7. namespace _13.ExchangeableWords
  8. {
  9.     class ExchangeableWords
  10.     {
  11.         static void Main()
  12.         {
  13.             string[] tokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  14.             string word1 = tokens[0];
  15.             string word2 = tokens[1];
  16.  
  17.  
  18.             if (word1.Length >= word2.Length)
  19.             {
  20.                 MapWords(word1, word2);
  21.             }
  22.             else
  23.             {
  24.                 MapWords(word2, word1);
  25.             }
  26.  
  27.         }
  28.  
  29.         private static void MapWords(string word1, string word2)
  30.         {
  31.             Dictionary<char, char> map = new Dictionary<char, char>();
  32.  
  33.             for (int i = 0; i < word2.Length; i++)
  34.             {
  35.                 char firstWordChar = word1[i];
  36.                 char secondWordChar = word2[i];
  37.  
  38.                 if (!map.ContainsKey(firstWordChar))
  39.                 {
  40.                     map[firstWordChar] = secondWordChar;
  41.                 }
  42.                 else if(map[firstWordChar] != secondWordChar)
  43.                 {
  44.                     Console.WriteLine("false");
  45.                     return;
  46.                 }
  47.             }
  48.  
  49.             for (int i = word2.Length; i < word1.Length; i++)
  50.             {
  51.                 char firstWordChar = word1[i];
  52.  
  53.                 if (!map.ContainsKey(firstWordChar))
  54.                 {
  55.                     Console.WriteLine("false");
  56.                     return;
  57.                 }
  58.             }
  59.  
  60.             Console.WriteLine("true");
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement