Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Making Anagrams Challenge

Sharing is caring!

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, a and b, 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 a=cde and b=dcf, we can delete e from string a and f from string b so that both remaining strings are cd and dc which are anagrams.

Function Description
Complete the makeAnagram function in the editor below. 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 \le |a|, |b| \le 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.

Solution

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MakingAnagrams {

    static int makeAnagram(String a, String b) {
        int ans = 0;
        int[] countA = new int[26];
        int[] countB = new int[26];

        // Map chars to count
        for (int i = 0; i < a.length(); i++) {
            char ch = a.charAt(i);
            countA[ch - 'a'] += 1;
        }

        for (int i = 0; i < b.length(); i++) {
            char ch = b.charAt(i);
            countB[ch - 'a'] += 1;
        }

        for (int i = 0; i < countA.length; i++) {
            if (countA[i] > countB[i]) {
                ans += (countA[i] - countB[i]);
            } else if (countB[i] > countA[i]) {
                ans += (countB[i] - countA[i]);
            }
        }

        return ans;
    }

    public static void main(String[] args) throws FileNotFoundException {
        long startTime = System.currentTimeMillis();
        System.setIn(new FileInputStream(System.getProperty("user.home") + "/" + "in.txt"));

        Scanner scanner = new Scanner(System.in);
        String a = scanner.nextLine();
        String b = scanner.nextLine();
        int result = makeAnagram(a, b);
        long endTime = System.currentTimeMillis();
        System.out.println("Took " + (endTime - startTime) + " ms");
        System.out.println(result);
    }
}