My Code is Running exactly good in the IDE (I have used many test cases ) but still not able submit code. Please help !!!
Problem Statement:- CodeChef: Practical coding for everyone
#include
#include
#include
using namespace std;
bool check_lapindrom(string str){
string str1{}, str2{};
int mid, i, j, count1{0}, count2{0};
int length = str.length();
mid = length/2;
///Break Logic
if(length % 2 == 0){
str1 = str.substr(0, mid);
str2 = str.substr(mid, mid);
}
else{
str1 = str.substr(0, mid);
str2 = str.substr((mid+1), mid);
}
///Comparing Logic
for(i=0; i<(int)str1.length(); i++){
for(j=i; j<(int)str1.length(); j++){
if(str1[i]==str[j]){ count1++; }
}
for(auto c : str2){
if(str1[i] == c){ count2++; }
}
if(count1!=count2){ return false; }
count1 = 0; count2 = 0;
}
return true;
}
int main(){
int t; cin>>t;
vector testcases(t);
for(auto &str : testcases){
cin>>str;
if(check_lapindrom(str)){cout<<"YES"<<endl;}
else{ cout<<"NO"<<endl; }
}
return 0;
}
You must print no or yes words after get all testcases
f.ex.
…
vector testcases(t);
vector answers(t);
int n=0;
for(auto &str : testcases){
cin>>str;
if(check_lapindrom(str)){answers[n++]=“YES”;}
else{ answers[n++]=“NO”; }
}
for (int j=0;j<n;j++){
cout<<test[j]<<"\n";
}
I just Do as you give the solution(Really Thanks For That) But It still does not Works. I am stuck I have spend a lot of hours but can’t find what’s wrong with this please help…
I didn’t get your comparing logic. Can you just brief me?
In the Comparing String I have Two strings str1 and str2. (TestCase:- abcbca) str1 = abc, str2 = bca
The first for loop is to get the first character of str1 like (a)
Then I have two more loops :-
(1st inner loop count how much times (a) come in str1)
(2nd inner loop count How much times (a) comes in str2)
And If( count is same then continues else exit and return false).
That all
Please Help!
After getting the two substrings, you can simply sort them and then compare them if they are equal or not!! It’s as simple as that.
Code Refer to it if still confused.
But you’ve started the first inner loop from j=i.
Suppose you have aabaab.
First character is a; i=0 and j will start from 0. count1 will be 2 and so will be count2.
Second character is also a; i=1 and j will start from 1. This time, count1 = 1 but count2 = 2.
1 Like
Yes This Idea Come to me But I just Wanted to Try this logic of Comparing.
Thanks a lot for your suggestion.
Thanks A Lot It solves My problem finally. Thankuuuuu