Last active
February 24, 2019 11:58
-
-
Save jonasraoni/2046247d85ca411eb981fe11209c4a89 to your computer and use it in GitHub Desktop.
Twin Strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) { | |
/* | |
* Creates an a-z dictionary, each key holds the quantity of even and odd positions where the character was found. | |
* As there's a same length check, if the required quantity of even/odd is not available, so it's not a twin. | |
*/ | |
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 int['z' - 'a' + 1, 2]; | |
for (var j = -1; ++j < itemB.Length;) | |
{ | |
++map[itemB[j] - 'a', j & 1]; | |
} | |
for (var j = -1; ++j < itemA.Length;) | |
{ | |
if (--map[itemA[j] - 'a', j & 1] < 0) | |
{ | |
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]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment