top of page
Writer's picturePrateek Chauhan

String Manipulation | Making Anagrams

Updated: May 30, 2020

In this blog we will discuss a problem about making anagrams.

You can also see this example here at HackerRank.

Here is the problem statement:


Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not. Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams.

Can you help her find this number? Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings. For example, if and , we can delete from string and from string so that both remaining strings are and which are anagrams.

Function Description: Complete the makeAnagram function. It must return an integer representing the minimum total characters that must be deleted to make the strings anagrams. makeAnagram has the following parameter(s):

  • a: a string

  • b: a string


Input Format: The first line contains a single string, a . The second line contains a single string, b .


Constraints:

  • 1 <= | a |, | b | <= 10^4

  • The strings a and b consist of lowercase English alphabetic letters ASCII [a-z].


Output Format: Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.



Sample Input:

cde 
abc 

Sample Output:

4 

Explanation: We delete the following characters from our two strings to turn them into anagrams of each other:

  1. Remove d and e from cde to get c.

  2. Remove a and b from abc to get c.

We must delete 4 characters to make both strings anagrams, so we print 4 on a new line.

 

Implementation using java:

import java.util.Scanner;

public class Solution {
    public static int numberNeeded(String a, String b) {
        /*We can just have an array of size 26, each letter                 
       representing a number. 
        
        We iterate through each of the strings, and count the number of             
        times each letter appears.
        
        To find the number of letters we need to make an anagram we 
        just need to find the differences between each bucket
        */
       int count[] = new int[26];
        for(char ch : a.toCharArray())
        {
            count[ch-97]++;
        }
        for(char ch : b.toCharArray())
        {
            count[ch-97]--;
        }
        int result =0;
        for(int i : count)
        {
            result =result+Math.abs(i);
            
        }
      return result;
    }
  
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.next();
        String b = in.next();
        System.out.println(numberNeeded(a, b));
    }
}
Output:
abcdeff
abcd
3
 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Follow Programmers Door for more.


11 views0 comments

Recent Posts

See All

Comments


bottom of page