Yet Another Palindrome Making Problem Codechef Solution
MAKEPALAGAIN
(100/100) Full Result
Codechef July Lunchtime | AC Code
Chef has a string
is even. He can perform the following operation any number of times:
- Swap
Ai andAi+2 for any1≤i≤(N−2)
Determine if Chef can convert string
Note: A string is called a palindrome if it reads the same backwards and forwards. For example,
Input Format
- The first line contains a single integer
T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer
N — the length of the string A. - The second line of each test case contains a string
A of lengthN containing lowercase Latin letters only.
Output Format
For each test case, output YES
if Chef can convert the string
to a palindromic string. Otherwise, output NO
.
You may print each character of YES
and NO
in either uppercase or lowercase (for example, yes
, yEs
, Yes
will be considered identical).
Constraints
1≤T≤200 1≤N≤1000 S contains lowercase Latin letters only.N is even
Sample Input 1
3
6
aabbaa
4
abcd
6
zzxyyx
Sample Output 1
YES
NO
YES
Explanation
Test case
The given string is already a palindrome.
Test case
It can be proven that is it not possible to convert
to a palindromic string.
Test case
We can perform the following operations:
- Swap
A1 andA3 . (NowA becomesxzzyyx
) - Swap
A2 andA4 . (NowA becomesxyzzyx
)
li n;
cin >> n;
string str;
cin >> str;
unordered_map mp1;
unordered_map mp2;
for (li i = 0; i < n; i++)
{
if (i == 0 || i % 2 == 0)
{
mp1[str[i]]++;
}
else
{
mp2[str[i]]++;
}
}
if (mp1 == mp2)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
0 Comments