Advertisement
alestori

MagicExchangeableWords

Oct 19th, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5.  
  6. class MagicExchangeableWords
  7. {
  8. static void Main()
  9. {
  10. {
  11. string[] input = Console.ReadLine().Split(' ');
  12. string first = input[0];
  13. string second = input[1];
  14.  
  15. Console.WriteLine(AreEqual(first, second) ? "true" : "false");
  16. }
  17. }
  18.  
  19.  
  20. private static bool AreEqual(string a, string b)
  21. {
  22. bool areEqual = true;
  23. var data = new Dictionary<char, char>();
  24. string aa = "";
  25. string bb = "";
  26. if (a.Length == b.Length)
  27. {
  28. for (int i = 0; i < a.Length; i++)
  29. {
  30. char firstChar = a[i];
  31. char secondChar = b[i];
  32. if (!data.ContainsKey(firstChar))
  33. {
  34. data.Add(firstChar, secondChar);
  35. }
  36. else
  37. {
  38. if (!data.ContainsValue(secondChar))
  39. {
  40. areEqual = false;
  41. }
  42. }
  43. }
  44. }
  45. else if (a.Length != b.Length)
  46. {
  47. int minLength = Math.Min(a.Length, b.Length);
  48. int maxLength = Math.Max(a.Length, b.Length);
  49. if (minLength == a.Length)
  50. {
  51. char[] arrA = a.ToCharArray();
  52. char[] arrALong = new char[maxLength];
  53. for (int i = 0; i < maxLength; i++)
  54. {
  55. arrALong[i] = arrA[i % minLength];
  56. }
  57. aa = new string(arrALong);
  58. bb = b;
  59. }
  60. else if (minLength == b.Length)
  61. {
  62. char[] arrB = b.ToCharArray();
  63. char[] arrBLong = new char[maxLength];
  64. for (int i = 0; i < maxLength; i++)
  65. {
  66. arrBLong[i] = arrB[i % minLength];
  67. }
  68. bb = new string(arrBLong);
  69. aa = a;
  70. }
  71. for (int i = 0; i < aa.Length; i++)
  72. {
  73. char firstChar = aa[i];
  74. char secondChar = bb[i];
  75. if (!data.ContainsKey(firstChar))
  76. {
  77. data.Add(firstChar, secondChar);
  78. }
  79. else
  80. {
  81. if (!data.ContainsValue(secondChar))
  82. {
  83. areEqual = false;
  84. }
  85. }
  86. }
  87. }
  88. else
  89. {
  90. areEqual = false;
  91. }
  92.  
  93. return areEqual;
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement