0

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

svampire
  • 11
  • 1
  • Please don't do `#include`. It's bad practice (along with `using namespace std;`) and on top of that, it's not supported by all implementations. If I copy your code, there's errors everywhere and I would have to go and guess all the headers first before I could try to find what's wrong with it. – Blaze Dec 21 '18 at 15:13
  • @Blaze "along with `using namespace std;`" that depends! Don't put everything into the same basket. – gsamaras Dec 21 '18 at 15:22
  • Just the other day a user here had issues with his code that wouldn't compile and generate weird errors, which were because one of his variables called `count` conflicted with one of the plentiful things in `std`. I can't recommend `using namespace std;` to anyone on the grounds that it is basically setting up a minefield for a benefit that is questionable as best. – Blaze Dec 21 '18 at 15:29

0 Answers0