Help me in solving ABCC problem

My issue

find issue in this problem i have calculated the index of 1st and last ‘b’ for which no of pairs of ac before last ‘b’ and pairs of ca after 1st ‘b’ as replacements to be equal i calculated like this please help.

My code


#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int m;
	cin>>m;
	while(m--)
	{
	   int n;
	   cin>>n;
	   string s,t;
	   cin>>s>>t;
	   if(s==t)cout<<"Yes"<<endl;
	   else{
	   int ac=0,ca=0;
	//    vector<int>sb;
	//    vector<int>tb;
		int idx1=0;
	   for (int i = 0; i < n; i++)
	   {
		if(s[i]=='b'){
			idx1=i;
			break;
		}
	   }
	   reverse(s.begin(),s.end());
	   int idx2=0;
	   for (int i = 0; i < n; i++)
	   {
		if(s[i]=='b'){
			idx2=i;
			break;
		}
	   }
	   reverse(s.begin(),s.end());
	   for (int i = 0; i < (n-1)-idx2; i++)
	   {
		if((s[i]=='a'&& t[i]=='c'))ac++;
	   }
	   for (int i = idx1; i < n; i++)
	   {
		if((s[i]=='c'&& t[i]=='a'))ca++;
	   }
	//    cout<<sb[sb.size()-1]<<endl;
	//    cout<< ac <<" "<< ca <<endl;
	   if(ac==ca && ac!=0)cout<<"Yes"<<endl;
	   else cout<<"No"<<endl;	
	   }
	}

}

Problem Link: ABC Conjecture Practice Coding Problem - CodeChef

let the testcase be
string s=“ababcba”;
string t=“cbacaba”;
for your code ac=1,ca=1;
so.you will print yes but No will be the answer as you cannot change the bold character

1 Like