#include <iostream>
#include <string.h>
using namespace std;
int main() {
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
char s[1001],a[501],b[501];
cin>>s;
if(strlen(s)%2==0)
{
int c=0,x,y;
for(x=0;x<strlen(s)/2;x++)
a[x]=s[x];
a[x]='\n';
for(y=x;y<strlen(s);y++)
b[y-x]=s[y];
b[y-x]='\n';
for(char z='a';z<='z';z++)
{
int count=0,count1=0;
for(int p=0;p<strlen(a);p++)
{
if(a[p]==z)
count++;
}
for(int p=0;p<strlen(b);p++)
{
if(b[p]==z)
count1++;
}
{
if(count!=count1)
{
c=1;
}
}
}
if(c==0)
cout<<"YES\n";
else
cout<<"NO\n";
}
else
{
int c=0,x,y;
for(x=0;x<strlen(s)/2;x++)
a[x]=s[x];
a[x]='\n';
for(y=x+1;y<strlen(s);y++)
b[y-x-1]=s[y];
b[y-x-1]='\n';
for(char z='a';z<='z';z++)
{
int count=0,count1=0;
for(int p=0;p<strlen(a);p++)
{
if(a[p]==z)
count++;
}
for(int p=0;p<strlen(b);p++)
{
if(b[p]==z)
count1++;
}
{
if(count!=count1)
{
c=1;
}
}
}
if(c==0)
cout<<"YES\n";
else
cout<<"NO\n";
}
}
return 0;
}`
you can take string as input
two array a[26]={0} and b[26]={0}
char letters[27] = “abcdefghijklmnopqrstuvwxyz”;
for(i=0;i<n/2;i++)
{
for(j=0;j<26;j++)
{
if(str[i]==letters[j])
{
a[j]++;
count++;
}
if(count>0)
continue;
}
}
if(n%2!=0)
n=n+1;
for(i=n/2;i<temp;i++)
{
for(j=0;j<26;j++)
{
if(str[i]==letters[j])
{
b[j]++;
count++;
}
if(count>0)
continue;
}
}
for(i=0;i<26;i++)
{
if(a[i]==b[i])
check++;
}
if(check==26)
printf("YES\n");
else
printf("NO\n");
this is a good approach toward this problem.(not full solution only logic)
You’ve got variations of
a[x]='\n';
b[y-x]='\n';
in your code; if this is intended to make strlen(a)
and strlen(b)
return the correct values, it won’t work (strlen
looks for null characters, not line-breaks)
3 Likes