To top off our conversation, let’s tackle our most complex recursive problem yet. We’re going to use everything we’ve got in our recursion toolbox to make this work.

We’re going to write a function that returns an array of all anagrams of a given string. An anagram is a reordering of all the characters within a string. For example, the anagrams of "abc" are:

​  ["abc",
​  "acb",
​  "bac",
​  "bca",
​  "cab",
​  "cba"]

Now, let’s say we were to collect all the anagrams of the string "abcd" . Let’s apply our top-down mindset to this problem.

Presumably, we could say that the subproblem of "abcd" is "abc" . The question then, is: if we had a working anagrams function that returned all the anagrams of "abc" , how can we use them to produce all the anagrams of "abcd" ? Think about this for a bit and see if you can come up with any approaches.

Here’s the approach that occurred to me. (There are others, though.)

If we had all six anagrams of "abc" , we can come up with every permutation of "abcd" if we stick the "d" in every possible spot within each anagram of "abc" :

images/learning_to_write_in_recursive/d_anagram_placement.png

Here is a Ruby implementation of this algorithm. You’ll note that it’s certainly more involved than the previous examples in this chapter:

​  ​ def ​ ​ anagrams_of ​(string)
​  ​ # Base case: if the string is only one character, ​
​  ​ # return an array containing just a single-character string: ​
​  ​ return ​ [string[0]] ​ if ​ string.​ length ​ == 1
​  
​  ​ # Create an array to hold all the anagrams: ​
​  collection = []
​  
​  ​ # Find all anagrams of the substring from the second character until ​
​  ​ # the end. For example, if the string is "abcd", the substring is "bcd", ​
​  ​ # so we'll find all anagrams of "bcd": ​
​  substring_anagrams = anagrams_of(string[1, string.​ length ​ - 1])
​  
​  ​ # Iterate over each substring ​
​  substring_anagrams.​ each ​ ​ do ​ |substring_anagram|
​  
​  ​ # Iterate over each index of the substring, from 0 until ​
​  ​ # one index past the end of the string: ​
​  (0..substring_anagram.​ length ​).​ each ​ ​ do ​ |index|
​  
​  ​ # Create a copy of the substring anagram: ​
​  copy = String.​ new ​(substring_anagram)
​  
​  ​ # Insert the first character of our string into the ​
​  ​ # substring anagram copy. Where it will go depends ​
​  ​ # on the index we're up to within this loop. ​
​  ​ # Then, take this new string and add it to our collection of anagrams: ​
​  collection << copy.​ insert ​(index, string[0])
​  ​ end ​
​  ​ end ​
​  
​  ​ # Return the entire collection of anagrams: ​
​  ​ return ​ collection
​  ​ end ​

This code is not trivial, so let’s break it down. For now, we’ll skip over the base case.

We start by creating an empty array in which we’ll collect the entire collection of anagrams:

​  collection = []

This is the same array we’ll return at the end of our function.

Next, we grab the array of all anagrams from the substring of our string. This substring is the subproblem string, namely, from the second character until the end. For example, if the string is "hello" , the substring is "ello" :

​  substring_anagrams = anagrams_of(string[1, string.​ length ​ - 1])

Note how we use the top-down mentality to assume that the anagrams_of function already works on the substring.

We then iterate over each of the substring_anagrams :

​  substring_anagrams.​ each ​ ​ do ​ |substring_anagram|

Before moving on, it’s worth noting at this point that we are using a combination of loops and recursion together. Using recursion doesn’t mean that you have to eliminate loops from your code altogether! We’re using each tool in the way that most naturally helps us solve the problem at hand.

For each substring anagram, we iterate over all of its indexes, make a copy of the substring anagram, and insert the first character of our string (the only character not contained within the substring) into that index. Each time we do this, we have created a new anagram, which we then add to our collection :

​  (0..substring_anagram.​ length ​).​ each ​ ​ do ​ |index|
​  copy = String.​ new ​(substring_anagram)
​  collection << copy.​ insert ​(index, string[0])
​  ​ end ​

When we’re done, we return the collection of anagrams.

The base case is where the substring contains only one character, in which case there’s only one anagram—the character itself!

The Efficiency of Anagram Generation

As an aside, let’s stop for a moment to analyze the efficiency of our anagram-generating algorithm, since we’ll discover something interesting. In fact, the time complexity of generating anagrams is a new category of Big O that we haven’t encountered before.

If we think about how many anagrams we generate, we’ll notice an interesting pattern.

For a string containing three characters, we create permutations that start with each of the three characters. Each permutation then picks its middle character from one of the two remaining characters, and its last character from the last character that’s left. This is 3 * 2 * 1, which is six permutations.

Looking at this for other string lengths, we get:

​  4 characters: 4 * 3 * 2 * 1 anagrams
​  5 characters: 5 * 4 * 3 * 2 * 1 anagrams
​  6 characters: 6 * 5 * 4 * 3 * 2 * 1 anagrams

Do you recognize this pattern? It’s a factorial!

That is, if the string has six characters, the number of anagrams is whatever the factorial of 6 is. This is 6 * 5 * 4 * 3 * 2 * 1 , which computes to 720.

The mathematical symbol for factorial is the exclamation point. So, factorial 6 is expressed as 6!, and the factorial of 10 is expressed as 10!.

Remember that Big O expresses the answer to the key question: if there are N data elements, how many steps will the algorithm take? In our case, N would be the length of the string.

For a string of length N, we produce N! anagrams. In Big O Notation then, this is expressed as O(N!) . This is also known as factorial time.

O(N!) is the slowest category of Big O we’ll encounter in this book. Let’s see how it looks compared to other "slow" Big O categories in the graph .

images/learning_to_write_in_recursive/big_o_factorial_graph.png

Although O(N!) is extremely slow, we don’t have a better option here, since our task is to generate all the anagrams, and there simply are N! anagrams for an N-character word.

In any case, recursion played a pivotal role in this algorithm, which is an important example of how recursion can be used to solve a complex problem.