Test Case for DSA Learning Series

Hi, I am getting the correct answer for the testcase given in problem here: CodeChef: Practical coding for everyone

But, getting wrong answer on submission. Nor can I see the testcase for which answer is coming wrong.

Here is my code:
#include
using namespace std;
int main() {
int t; cin>>t;
while(t–){
string x; cin>>x;
int l=x.length(); string a1, a2;
int p=0;int q=0;
for(int i=0; i<l/2; i++){ p+=(int)x[i];}
if(l%2==0){ for(int i=l/2; i<l; i++){q+=(int)x[i];}}
else { for(int i=l/2+1; i<l; i++){ q+=(int)x[i];} }
//cout<<p<<" "<<q<<endl;
if(p==q)cout<<“YES”<<endl;
else cout<<“NO”<<endl;
}
return 0;
}

It would be a great help if someone could point out what’s wrong in this code, or provide a link to the testcases of this problem(if there’s any).


void lapindromes()
{
    std::string text;
    std::string ans;

    std::cin>>text;
    
    ans=text;
    
    if(text.size() % 2 == 0)
    {
        int sb = text.size();
        text.erase(0, sb/2);
        ans.erase(sb/2, sb);
    }
    else
    {
        int sb = text.size() + 1;
        text.erase(0, sb/2);
        ans.erase(sb/2 - 1, sb);
    }
    
    std::string ans2=ans;
    reverse(ans.begin(), ans.end());
    
    if(ans==text)
    {
        std::cout<<"YES\n";
    }
    else if(ans2==text)
    {
        std::cout<<"YES\n";
    }
    else
    {
        std::cout<<"NO\n";
    }
}

int main()
{
    int n;
    
    std::cin>>n;
    
    for(int i = 0 ; i < n ; i++ )
    {
        lapindromes();
    }
    return 0;
}```

My code also doesn’t work

@codetoplay try to compare the number of each characters frequency of string a1 with a2.
for example,
If we take input string as fbag(which is not a lapindrome)
but your code will output it as “YES” because it’s counting the ASCII values.
i.e. value of p is f+b = 6+2 = 8 and value of q is a+g = 1+7 = 8,
So it gives the wrong output.
So sort the strings and then compare them.

Hope this answers your question

Your approch is wrong. We have to match frequency of each letter but you are just making a comparison.