The following description is taken from CodeChef.com's Lapindrome problem.
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 halvesga
andga
have the same characters with same frequency. Also,abccab
,rotor
andxyzxy
are a few examples of lapindromes. Note thatabbaab
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:
First line of input contains a single integer
T
, the number of test cases.Each test is a single line containing a string
S
composed of only lowercase English alphabet.Output:
For each test case, output on a separate line: "
YES
" if the string is a lapindrome and "NO
" if it is not.Constraints:
\$1 ≤ T ≤ 100\$
2 \$≤ |S| ≤ 1000\$, where|S|
denotes the length ofS
.Example:
Input:
6 gaga abcde rotor xyzxy abbaab ababc
Output:
YES NO YES YES NO NO
#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char s[1005];
scanf("%s",s);
int len,b[strlen(s)],z;
int i,j,count =0;
len=strlen(s);
for(z=0;z<len;z++)
{
b[z] =1;
}
if(len%2 !=0)
{
for(i=0;i<len/2;i++)
{
for(j=(len/2)+1;j<len;j++)
{
if(s[i] == s[j])
{
if(b[i] && b[j] )
{
b[i]=0;
b[j]=0;
count+=1;
}
}
}
}
}
else if(len%2 ==0)
{
for(i=0;i<len/2;i++)
{
for(j=(len/2);j<len;j++)
{
if(s[i] == s[j])
{
if(b[i] && b[j] )
{
b[i]=0;
b[j]=0;
count+=1;
}
}
}
}
}
if(count==len/2)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
}