welcome to


Cracking the Coding Interview

Strings: Making Anagrams

Located here: https://www.hackerrank.com/challenges/ctci-making-anagrams


This example is done in Python.

The problem states the following:

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.

Input Format:

The first line contains a single string, a.

The second line contains a single string, b.


len(a) >= 1

len(b) <= 1000

It is guaranteed that and consist of lowercase English alphabetic letters (i.e., a through 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


Sample Output



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

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

We are given:

def number_needed(a, b):

a = raw_input().strip()
b = raw_input().strip()

print number_needed(a, b)

I ended up solving this before getting to blog about it, but I will practice stepping through my thought process.

def number_needed(a, b):
    a_chars = {}
    b_chars = {}
    for c in a:
        a_chars[c] = a_chars.get(c, 0) + 1
    for c in b:
        b_chars[c] = b_chars.get(c, 0) + 1
    for k in a_chars.keys():
        for k2 in b_chars.keys():
            if k == k2:
                a_chars[k] = a_chars[k] - b_chars[k2]
                b_chars[k2] = 0 
    d = 0
    for k in a_chars.keys():
        d = d + a_chars[k]
    for k in b_chars.keys():
        d = d + b_chars[k]
    return d

tl;dr: Take each string, use each letter as a key to a dictionary for each string to count how many of each letter is in each string, then find matching keys in each dictionary. Store the distance between each letter count in the first dictionary and zero-out the second. Cycle through each dictionary and count the remaining occurences. Return the sum.

  1. It first occured to me that each string's character-count could be represented as a dictionary. I've done this problem before.
  2. When you understand higher-level structures, but it has been a while since you've gotten warm with a language, you don't realize some mechanisms available, such as a_dict.get(c,0)
  3. Using for char_c in string_a is highly useful.