Help me in solving LCPESY problem

My issue

#include<bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t–){
string a,b;
int c=0;
cin>>a>>b;
int n=a.length();
int m = b.length();
sort(a.begin(),a.end());
sort(b.begin(),b.end());
for(int i=0,j=0;i<n,j<m;i++,j++){
if(a[i]==b[j])
c++;
}
cout<<c<<endl;
}
return 0;
}
Please help me to detect the error in the code. Sample test cases are showing correct output but may be some hidden test cases showing incorrect result.

My code

#include<bits/stdc++.h>
using namespace std;
int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--){
	    string a,b;
	    int c=0;
	    cin>>a>>b;
	    int n=a.length();
	    int m = b.length();
	    sort(a.begin(),a.end());
	    sort(b.begin(),b.end());
	    for(int i=0,j=0;i<n,j<m;i++,j++){
	       if(a[i]==b[j])
	       c++;
	    }
	        cout<<c<<endl;
	}
	return 0;
}

Problem Link: Longest Common Pattern Practice Coding Problem - CodeChef

@harshit_232002
Your logic is not fully correct
like for ex:-
aaabbb
bbb
your answer would be 0
but the corrrect answer would be 3
plzz refer my c++ code for the better understanding of the logic

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

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    string a,b;
	    cin>>a>>b;
	    map<char,int> mp;
	    int ans=0;
	    for(int i=0;i<a.size();i++)
	    {
	        mp[a[i]]++;
	    }
	    for(int i=0;i<b.size();i++)
	    {
	        if(mp[b[i]]>0)
	        {
	            mp[b[i]]--;
	            ans++;
	        }
	    }
	    cout<<ans<<endl;
	}
	return 0;
}
1 Like

Yes, Thank you very much!
Didn’t consider the test case as mentioned in the example.
You got my problem solved. Once again Thanks.