Introduction
Scrabble is a popular board game where players attempt to find highest scoring words given a scramble of letters.My partner is far better at Scrabble than I, but I am a far better programmer than her. Therefore, using my craft of coding, I have built an unscrambler so that I can cheat and be the best Scrabble player in the world.
This post is to show you a (one-of-many) solution to find English words from a scramble letters.
The steps to solve this problem are:
1. Find every permutation of a string
2. Find the highest scoring English words out of all permutations
This post will only be covering step 1. I'll cover step 2 in my next post.
Find Every Permutation of a String
Let's find all permutations of the string "abcd"To do this, I loop through each character in the string, remove it from the string, and then proceed to place that character in each location within the remainder of the string.
For example:
I remove the first character (a) from the string, leaving the remainder of the string (bcd).
Then I move the character (a) throughout (bcd), and I repeat this for each character in the original string (abcd)
As shown below:
a, bcd
|
b, acd
|
c, abd
|
d, abc
|
abcd
|
bacd
|
cabd
|
dabc
|
bacd
|
abcd
|
acbd
|
adbc
|
bcad
|
acbd
|
abcd
|
abdc
|
bcda
|
acdb
|
abdc
|
abcd
|
However, as you may realise, the table above does not contain every permutation of (abcd). This is because the remainder string also needs to be reshuffled in every possible way when placing the character through it.
For example:
(c) (abd)
After moving (c) through (abd), I need to reshuffle (abd) following the same process. As follows:
(a), (bd)
I remove (a) from the string, leaving (bd), and then I move the (a) through the string.
abd
bad
bda
And then for b:
(b), (ad)
bad
abd
adb
etc..
And after each shuffle, I pass the first removed character (c) through it.
The same must also be done for the remainder of the string (bd).
(I hope you get the picture)
(bd) can be reshuffled into two possibilities (bd and db), and there is no need to reshuffle strings any smaller than this.
So, to achieve the recursive behaviour above, we will need to write a recursive method, that is capable of reordering a string, whilst extracting each possible permutation into a result set.
The final result should look like this:
a
ab
abc
abcd
abd
abdc
ac
acb
acbd
acd
acdb
ad
adb
adbc
adc
adcb
|
b
ba
bac
bacd
bad
badc
bc
bca
bcad
bcd
bcda
bd
bda
bdac
bdc
bdca
|
c
ca
cab
cabd
cad
cadb
cb
cba
cbad
cbd
cbda
cd
cda
cdab
cdb
cdba
|
d
da
dab
dabc
dac
dacb
db
dba
dbac
dbc
dbca
dc
dca
dcab
dcb
dcba
|
The Code
I first create a static class called StringPermutations with one public method:public static IList<string> GetAllPermutations(this string text)
Note: By using the keyword "this", I am intending to create an extension method.
The goal of this method is to take a string as a parameter and to return a list of all permutations.
Here is the content of that method:
public static IList<string> GetAllPermutations(this string text) { _finalPermutations = new List<string>(); GetPermutations(text); return _finalPermutations; }
There isn't much going on here, we initialise a _finalPermutations field, call a private method called GetPermutations, passing the input string into it and then returning _finalPermutations.
This is where the magick happens:
private static IList<string> GetPermutations(string letters) { var permutations = new List<string>();
if (letters.Length == 1) { if (!_finalPermutations.Contains(letters)) { _finalPermutations.Add(letters); } return new List<string> { letters }; }
for (int i = 0; i < letters.Length; i++) { var c = letters[i]; var remainingLetters = letters.Remove(i, 1); var childPermutations = GetPermutations(remainingLetters); foreach (var childPermutation in childPermutations) { for (int x = 0; x < childPermutation.Length; x++) { var newPermutation = childPermutation.Insert(x, c.ToString()); if (!permutations.Contains(newPermutation)) { permutations.Add(newPermutation); } if (!_finalPermutations.Contains(newPermutation)) { _finalPermutations.Add(newPermutation); } } } } return permutations; }
Firstly, I validate the letters parameter and check whether the size of the string is 1. If so, then we have reached the lowest point of recursion. I add the letter to the _finalPermutations list and then return the method, so that we climb back up the recursion ladder.
If the letters parameter has 2 characters or more then we proceed to loop through all characters.
I then remove that character from the string and store the remainder of the string in a variable called remainingLetters
And then using recursion, I self invoke GetPermutations passing the remainder of the string (remainingLetters), in order to find all possibilities of the remaining string.
Once the method returns (which will be once the recursion reaches one character in length), I then loop through each permutation passing the original extracted character at every position, whilst storing the permutation in two result sets. permutations is for returning from the method, passing permutation back up the recursion chain, and _finalPermutations is for storing all permutations.
An Alternative Idea
Another approach suggested by a colleague is to create my own base number system and simply by counting in that base I can create all combinations. For example:
A string containing 4 characters "abcd".
I create a base 4 number system.
I map each letter to each number in that base
(a=1, b=2. c=3, d=4)
I then count:
1, 2, 3, 4
11, 12, 13, 14
21, 22, 23, 24
31, 32, 33, 34
41, 42, 43, 44
111, 112, 113, 114
etc..
I like the idea and may implement it and share in a blog post some time.
I hope this makes sense, if you wish for me to explain anything then please leave a comment. I'm also interested in your suggestions on how I can improve this code.
In my next post, I will show how I used the Hunspell library (the spellchecker behind OpenOffice) to check each permutation for an English word.
Thanks