Lapindrome is defined as a string which when split in the middle, gives two halves having the same characters and same frequency of each character. If there are odd number of characters in the string, we ignore the middle character and check for lapindrome. For example gaga is a lapindrome, since the two halves ga and ga have the same characters with same frequency. Also, abccab, rotor and xyzxy are a few examples of lapindromes. Note that abbaab is NOT a lapindrome. The two halves contain the same characters but their frequencies do not match.
Your task is simple. Given a string, you need to tell if it is a lapindrome.
Input:
Each test is a single line containing a string S composed of only lowercase English alphabet.
Output:
Constraints:
- 1 ≤ T ≤ 100
- 2 ≤ |S| ≤ 1000, where |S| denotes the length of S
Sample Input 1
6
gaga
abcde
rotor
xyzxy
abbaab
ababc
Sample Output 1
YES
NO
YES
YES
NO
NO
___________________________________________________________________
solution in cpp
#include<iostream>
#include<cmath>
#define ll long long
#include<vector>
#include<stack>
#include<climits>
#include<algorithm>
using namespace std;
void solve()
{
string s;
cin >> s;
ll n = s.length();
string s1, s2;
for (ll i = 0; i < n/2; i++)
{
s1 += s[i];
s2 += s[n - 1 - i];
}
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if (s1 == s2)
cout << "YES\n";
else
cout << "NO\n";
}
int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
No comments:
Post a Comment