Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Twin Strings
- 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:
- SwapEven: Swap a character at an even-numbered index with a character at another even-numbered index.
- SwapOdd: Swap a character at an odd-numbered index with a character at another odd-numbered index.
- 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).
- Complete the twins function in the provided code. It has two parameters:
- An array of n strings named a.
- An array of n strings named b.
- 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.
- Input Format
- The provided code reads the following input from stdin and passes it to the function:
- The first line contains an integer, n, denoting the number of elements in a.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing ai.
- The next line contains an integer, n, denoting the number of elements in b.
- Each line i of the n subsequent lines (where 0 ≤ i < n) contains a string describing bi.
- Constraints
- 1 ≤ n ≤ 10^3
- 1 ≤ lengths of ai, bi ≤ 100
- ai and bi are not guaranteed to have the same length.
- Strings ai and bi contain lowercase letters only (i.e., a through z).
- Output Format
- 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.
- Sample Input 0
- 2
- cdab
- dcba
- 2
- abcd
- abcd
- Sample Output 0
- Yes
- No
- */
- namespace Trial {
- class Program {
- /*
- * Complete the function below.
- * DO NOT MODIFY CODE OUTSIDE THIS FUNCTION!
- */
- static string[] twins(string[] a, string[] b) {
- var r = new string[a.Length];
- for (var i = -1; ++i < a.Length;) {
- string itemA = a[i], itemB = b[i];
- var isTwin = itemA.Length == itemB.Length;
- if (isTwin){
- var map = new Dictionary<char, int[]>(itemA.Length);
- for (var j = -1; ++j < itemB.Length;) {
- if (!map.ContainsKey(itemB[j]))
- map[itemB[j]] = new int[2];
- ++map[itemB[j]][j & 1];
- }
- for (var j = -1; ++j < itemA.Length;) {
- if (map.ContainsKey(itemA[j]) && map[itemA[j]][j & 1] > 0)
- --map[itemA[j]][j & 1];
- else {
- isTwin = false;
- break;
- }
- }
- }
- r[i] = isTwin ? "Yes" : "No";
- }
- return r;
- }
- // DO NOT MODIFY CODE OUTSIDE THE ABOVE FUNCTION!
- static void Main(String[] args) {
- string[] res;
- int _a_size = 0;
- _a_size = Convert.ToInt32(Console.ReadLine());
- string[] _a = new string[_a_size];
- string _a_item;
- for (int _a_i = 0; _a_i < _a_size; _a_i++) {
- _a_item = Console.ReadLine();
- _a[_a_i] = _a_item;
- }
- int _b_size = 0;
- _b_size = Convert.ToInt32(Console.ReadLine());
- string[] _b = new string[_b_size];
- string _b_item;
- for (int _b_i = 0; _b_i < _b_size; _b_i++) {
- _b_item = Console.ReadLine();
- _b[_b_i] = _b_item;
- }
- res = twins(_a, _b);
- for (int res_i = 0; res_i < res.Length; res_i++) {
- Console.WriteLine(res[res_i]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment