Guest User

Untitled

a guest
Mar 18th, 2018
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. /*
  2. Twin Strings
  3. Two strings, a and b, are said to be twins only if they can be made equivalent by performing some number of operations on one or both strings. There are two possible operations:
  4.  
  5. SwapEven: Swap a character at an even-numbered index with a character at another even-numbered index.
  6. SwapOdd: Swap a character at an odd-numbered index with a character at another odd-numbered index.
  7.  
  8. For example, a = "abcd" and b = "cdab" are twins because we can make them equivalent by performing operations. Alternatively, a = "abcd" and b = "bcda" are not twins (operations do not move characters between odd and even indices), and neither are a = "abc" and b = "ab" (no amount of operations will insert a 'c' into string b).
  9.  
  10. Complete the twins function in the provided code. It has two parameters:
  11. An array of n strings named a.
  12. An array of n strings named b.
  13. The function must return an array of strings where each index i (0 ≤ i < n) contains the string Yes if ai and bi are twins or the string No if they are not.
  14.  
  15. Input Format
  16. The provided code reads the following input from stdin and passes it to the function:
  17. The first line contains an integer, n, denoting the number of elements in a.
  18. Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing ai.
  19. The next line contains an integer, n, denoting the number of elements in b.
  20. Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing bi.
  21.  
  22. Constraints
  23. 1 ≤ n ≤ 10^3
  24. 1 ≤ lengths of ai, bi ≤ 100
  25. ai and bi are not guaranteed to have the same length.
  26. Strings ai and bi contain lowercase letters only (i.e., a through z).
  27.  
  28. Output Format
  29. The function must return an array of strings where each index i (0 ≤ i < n) contains the string Yes if ai and bi are twins or the string No if they are not.
  30.  
  31. Sample Input 0
  32. 2
  33. cdab
  34. dcba
  35. 2
  36. abcd
  37. abcd
  38.  
  39. Sample Output 0
  40. Yes
  41. No
  42. */
  43.  
  44. namespace Trial {
  45. class Program {
  46. /*
  47. * Complete the function below.
  48. * DO NOT MODIFY CODE OUTSIDE THIS FUNCTION!
  49. */
  50. static string[] twins(string[] a, string[] b) {
  51. var r = new string[a.Length];
  52. for (var i = -1; ++i < a.Length;) {
  53. string itemA = a[i], itemB = b[i];
  54. var isTwin = itemA.Length == itemB.Length;
  55. if (isTwin){
  56. var map = new Dictionary<char, int[]>(itemA.Length);
  57. for (var j = -1; ++j < itemB.Length;) {
  58. if (!map.ContainsKey(itemB[j]))
  59. map[itemB[j]] = new int[2];
  60. ++map[itemB[j]][j & 1];
  61. }
  62. for (var j = -1; ++j < itemA.Length;) {
  63. if (map.ContainsKey(itemA[j]) && map[itemA[j]][j & 1] > 0)
  64. --map[itemA[j]][j & 1];
  65. else {
  66. isTwin = false;
  67. break;
  68. }
  69. }
  70. }
  71. r[i] = isTwin ? "Yes" : "No";
  72. }
  73. return r;
  74. }
  75. // DO NOT MODIFY CODE OUTSIDE THE ABOVE FUNCTION!
  76.  
  77. static void Main(String[] args) {
  78. string[] res;
  79.  
  80. int _a_size = 0;
  81. _a_size = Convert.ToInt32(Console.ReadLine());
  82. string[] _a = new string[_a_size];
  83. string _a_item;
  84. for (int _a_i = 0; _a_i < _a_size; _a_i++) {
  85. _a_item = Console.ReadLine();
  86. _a[_a_i] = _a_item;
  87. }
  88.  
  89.  
  90. int _b_size = 0;
  91. _b_size = Convert.ToInt32(Console.ReadLine());
  92. string[] _b = new string[_b_size];
  93. string _b_item;
  94. for (int _b_i = 0; _b_i < _b_size; _b_i++) {
  95. _b_item = Console.ReadLine();
  96. _b[_b_i] = _b_item;
  97. }
  98.  
  99. res = twins(_a, _b);
  100. for (int res_i = 0; res_i < res.Length; res_i++) {
  101. Console.WriteLine(res[res_i]);
  102. }
  103. }
  104. }
  105. }
Add Comment
Please, Sign In to add comment