I am triyng to solve a question which requires two strings and there are many test cases. i had written the code for problem considering many testcases. and when i take only one test case the code runs properly but when i enter more than one testcase it gives wrong answers
The code is for the question on hackerearth as below Given two strings, a and b , that may or may not be of the same length, determine the minimum number of character deletions required to make a and b anagrams. Any characters can be deleted from either of the strings.
Input :
test cases,t two strings a and b, for each test case Output:
Desired O/p
Constraints :
string lengths<=10000
Note :
Anagram of a word is formed by rearranging the letters of the word.
For e.g. -> For the word RAM - MAR,ARM,AMR,RMA etc. are few anagrams.
Here is my code in c++:
#include<bits/stdc++.h>
using namespace std;
int main()
{
//cin.tie(NULL);
//ios_base::sync_with_stdio(false);
int t;
//cout<<"Enter no of testcases : ";
cin>>t;
//cout<<endl;
for(int k=0;k<t;k++)
{
string s1;
string s2;
cin.ignore();
//cout<<"Enter first string : ";
getline(cin,s1);
//cout<<"Enter second string : ";
getline(cin,s2);
int c1[26],c2[26];
for(int itr='a';itr<'z';itr++)
{
c1[itr]=0;
c2[itr]=0;
}
for(int itr=0;itr<s1.length();itr++)
c1[s1[itr]]++;
for(int itr=0;itr<s2.length();itr++)
c2[s2[itr]]++;
int max=(s1.length()>s2.length()?s1.length():s2.length());
//cout<<endl<<max;
int analen=0;
for(int itr='a';itr<'z';itr++)
{
analen+=((c1[itr]<c2[itr])?c1[itr]:c2[itr]);
}
//cout<<"length of anagram is: "<<analen<<endl;
int del=(s1.length()+s2.length())-2*analen;
cout<</*"Number of characters required to delete to get an anagram is : "<<*/del;
}
return 0;
}
Input: 1 abc cde
Ouput: 4
for this above input the code runs perfectly but Input: 2 abc cde pqr rst
Output: 4 3
Correct Output: 4 4