We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Strings
  4. Making Anagrams
  5. Discussions

Making Anagrams

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 629 Discussions, By:

votes

Please Login in order to post a comment

  • anirudherabelly
    6 years ago+ 19 comments

    Java implementation of the problem!!

    import java.io.*;

    import java.util.*;

    import java.text.*;

    import java.math.*;

    import java.util.regex.*;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        String s1=sc.nextLine();
        String s2=sc.nextLine();
        int cArr[]=new int[26];
        int cArr1[]=new int[26];
        for(int i=0;i<s1.length();i++)cArr[s1.charAt(i)-97]++;
        for(int i=0;i<s2.length();i++)cArr1[s2.charAt(i)-97]++;
        int count=0;
        for(int i=0;i<26;i++)count+=Math.abs(cArr[i]-cArr1[i]);
        System.out.println(count);
    }
    

    }

    56|
    Permalink
    View more Comments..
  • DomNomNom
    6 years ago+ 12 comments

    4 clean lines - python3

    from collections import Counter
    counts = Counter(input())
    counts.subtract(input())
    print(sum(abs(x) for x in counts.values()))
    
    48|
    Permalink
    View more Comments..
  • snkt_pat2
    6 years ago+ 5 comments

    passed all test cases in 0s

    string in1,in2;
    cin>>in1>>in2;
    int array[26] = {0};
    for(int i=0;i<in1.length();i++)
        array[in1[i]-'a']++;
    for(int i=0;i<in2.length();i++)
        array[in2[i]-'a']--;
    int sum = 0;
    for(int i=0;i<26;i++)
        sum+=abs(array[i]);
    cout<<sum;
    return 0;
    
    26|
    Permalink
    View more Comments..
  • ankushrasgon07
    3 years ago+ 1 comment
    def makingAnagrams(s1, s2):
        for i in s1:
            if i in s2:
                s1=s1.replace(i,"",1)
                s2=s2.replace(i,"",1)
        return(len(s1)+len(s2))
    

    Here is my simple python 3 solution

    16|
    Permalink
  • ASHWIN_18
    5 years ago+ 1 comment

    simple

    def makingAnagrams(s1, s2):
        # Complete this function
        k=[c for c in s1]
        l=list()
        for m in s2:
            if m in k:
                k.remove(m)
            else:
                l.append(m)
        
        
        return (len(k)+len(l))
    
    9|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature