Anagrams - Hackerearth Solution

 


Anagrams


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.


Sample InputSample Output

1

cde

abc

4





  • Solution:
Python3.8
    s1=input().lower().strip()
    s2=input().lower().strip()
    set1=set(s1)
    set2=set(s2)
    y=len(sorted(set1.difference(set2)))
    z=len(sorted(set2.difference(set1)))
    print(z+y)




    C++14
      #include <bits/stdc++.h>
      using namespace std;
      int main()
      {
      int t;
      cin>>t;
      while(t--)
      {
      string a,b;
      int freq[26]={0};
      int count=0;
      cin>>a>>b;
      for(int i=0;i<a.length();i++)
      {
      for(int j=0;j<b.length();j++)
      {
      if(a[i]==b[j])
      {
      count++;
      b[j]='0';
      break;
      }
      }
      }
      int temp=a.length()+b.length()-2*count;
      cout<<temp<<endl;
      }
      }


      • Note:


        Note:

        This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

        Disclaimer:-

        The above hole problem statement is given by hackerearth.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.

        Anagrams - Hackerearth Solution Anagrams - Hackerearth Solution Reviewed by CodexRitik on November 17, 2020 Rating: 5

        No comments:

        Powered by Blogger.