try this:
1
abac
@insaynasasin imho, you tried to convert chars in your string array to ints. try left[arr[i]-‘a’], right[arr[i]-‘a’]
@garakchy no luck, it is still giving the right answers for the given testcases but on submission, its a wa
was trying since yesterday, lol, and i just got ac on the problem.
and i used memcmp, that is a bit faster than every array index comparison imho, try that also @insaynasasin
why is this wrong
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int t;
cin>>t;
vector<string> ans;
while(t--)
{
string str;
cin>>str;
while(str.length()>1)
{
int len = str.length();
int last_of_first_half;
int first_of_second_half;
int sum1,sum2;
sum1=sum2=0;
char ch = str[0];
if(len%2!=0)
{
last_of_first_half = len/2 -1 ;
first_of_second_half = len/2 +1 ;
}
else
{
last_of_first_half = len/2 -1 ;
first_of_second_half = len/2 ;
}
string temp="";
for(int i=0;i<=last_of_first_half;i++)
{
if(str[i]==ch)
sum1++;
else
{
temp+=str[i];
}
}
for(int j=first_of_second_half;j<len;j++)
{
if(str[j]==ch)
sum2++;
else
{
temp+=str[j];
}
}
if(sum1!=sum2)
break;
else
str=temp;
}
if(str.length()<2)
ans.push_back("YES");
else
ans.push_back("NO");
}
for(auto i=ans.begin();i!=ans.end();i++)
cout<<*i<<"\n";
return 0;
}
#include
#include
using namespace std;
int main()
{
int T;
cin>>T;
for(int i=0;i<T;i++)
{ int half;
int count=0;
string s;
cin>>s;
int len=s.length();
if(len%2 == 0)
{
half=len/2;
string s1[half];
string s2[half];
for(int j=0;j<half;j++)
{
s1[j]=s[j];
s2[j]=s[len-j-1];
}
for(int j=0;j<half;j++)
{for(int k=0;k<half;k++)
{
if(s1[j]==s2[k])
{
count++;
s1[j]=j;
s2[k]=j;
}
}}
if(count==half)
{
cout<<"YES";
}
else
{cout<<"NO";}
cout<<endl;
}
else
{
half=(len+1) /2;
string s1[half-1];
string s2[half-1];
for(int j=0;j<half-1;j++)
{
s1[j]=s[j];
s2[j]=s[len-j-1];
}
for(int j=0;j<half-1;j++)
{for(int k=0;k<half-1;k++)
{
if(s1[j]==s2[k])
{
count++;
s1[j]=j;
s2[k]=j;
}
}}
if(count==(half-1))
{
cout<<"YES";
}
else
{cout<<"NO";}
cout<<endl;
}
}
return 0;
}
i used different approach , no sorting no array declaration, is this best one compared to other ?
https://www.codechef.com/viewsolution/27532994
My code gets compiled and run successfully but it doesnt get accepted during submission
Can you please point out the reason why it is like that from the following code ?
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T=Integer.parseInt(br.readLine());
String ans="";
while(T>0){
String b=br.readLine();
ans+=solve(b);
T–;
}
System.out.print(ans);
}
static String solve(String x){
int len=x.length();
String com1=null;
String com2=null;
if(len%2==0){
com1=x.substring(0,len/2);
com2=x.substring(len/2,len);
}else{
com1=x.substring(0,len/2);
com2=x.substring((len/2)+1,len);
StringBuilder sb = new StringBuilder(com2);
String com3=sb.reverse().toString();
if(com1.equals(com3)){
return “YES\n”;
}
}
if(com1.contentEquals(com2))
return "YES\n";
else
return "NO\n";
}
}
Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! ![]()
Thank you for replying !! Here is the link to my submission:-CodeChef: Practical coding for everyone
Thanks - your submission fails for the following testcase:
1
abba
How did you check this?
i mean how did you know the reason for rejection of my submission!!
I usually write a testcase generator for the problems I solve - I droned on about the process here. It was then a simple matter to check the output from your program against known-correct answers to random testcases until one failed ![]()
Edit:
To be clear - I don’t know if Codechef run that particular abba testcase, but it’s definitely a testcase that exposes a flaw in your program ![]()
Okay thank you my friend for helping me out and teaching me new stuff. I have successfully removed that flaw from my code and tried to resubmit but still my submission gets rejected and its very frustrating. Here is the link of my new submission CodeChef: Practical coding for everyone
Please have a look in it.
This fails on
1
abbbabbab
I suggest re-reading the Problem very carefully, as your current approach isn’t going to work 
Okay thank you for helping me !! 