Problem Description
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:
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 of SCODING ARENA
#include <stdio.h>
#include <string.h>
int main()
{
int ans,l,t,i,c1,c2;
char s[1001],c;
scanf("%d",&t);
while(t--)
{
ans=1;
scanf("%s",s);
char s1[1001],s2[1001];
l=strlen(s);
c1=c2=0;
for(i=0;i<l/2;i++)
s1[c1++]=s[i];
if(l%2==1)
i=l/2+1;
else
i=l/2;
for(;i<l;i++)
s2[c2++]=s[i];
s1[c1]='\0';
s2[c2]='\0';
int ar1[26],ar2[26];
memset(ar1,0,26*sizeof(int));
memset(ar2,0,26*sizeof(int));
for(i=0;i<c1;i++)
{
c=s1[i];
ar1[(int)c-97]++;
}
for(i=0;i<c2;i++)
{
c=s2[i];
ar2[(int)c-97]++;
}
for(i=0;i<26;i++)
{
if(ar1[i]!=ar2[i])
{
ans=0;
break;
}
}
if(ans==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
Test Case 1
Input (stdin)6 gaga abcde rotor xyzxy abbaab ababc
Expected OutputYES NO YES YES NO NO
Test Case 2
Input (stdin)1 gaga
Expected OutputYES
No comments:
Post a Comment