Click here to Skip to main content
15,885,216 members
Articles / General Programming / Algorithms
Tip/Trick

How to Unscramble Any Word

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
12 Apr 2021CPOL1 min read 15.5K   12   31
This is an unscramble class that can be used to decypher any word.
I could never find a good way to unscramble a word on the interwebs. Every algorithm was either brute-force or permutations...

Introduction

I could never find a good way to unscramble a word on the interwebs. Every algorithm was either brute-force or permutations.

The problem with brute-force is that it's a guessing game... very slow, or if you're lucky, very fast.

The problem with permutations is that once you go over 7 characters, you're using a bunch of memory.
e.g., a 12 character scrambled word has 479,001,600 configurations!

It finally dawned on me that if you sort the scrambled word and then sort the dictionary entries, then if we equate any sorted dictionary entry to our sorted scramble, then they must be a match!

There is probably some fancy machine learning algorithm that could do this, but my method works perfectly and instantly.

Using the Code

You'll want to embed your favorite dictionary into your project (for speed and portability).

There are a lot of free dictionary files out there; here's the one I used... https://github.com/dwyl/english-words.

Direct link... https://raw.githubusercontent.com/dwyl/english-words/master/words.txt.

The work-horse is the UnscrambleWord method; this will take care of loading the dictionary, filtering and then sorting the results and storing them in a List<string> object that will be returned to you from the call.

C#
class Unscramble
{
     private static bool _dictionaryLoaded = false;
     private static string _wordToUnscramble = "";
     private static int _totalEntries = 0;
     private static Dictionary<string, string> _sortedDictionary =
                                       new Dictionary<string, string>();
     private static List<string> _results = new List<string>();
     private static Stopwatch _stopwatch;

     //====================================================================================
     /** We don't really need a constructor **/
     //public Unscramble(string wordToUnscramble)
     //{
     //    _WordToUnscramble = wordToUnscramble;
     //}

     //====================================================================================
     public List<string> UnscrambleWord(string wordToUnscramble, bool useFiltering = true)
     {
         _stopwatch = Stopwatch.StartNew();

         if (string.IsNullOrEmpty(_wordToUnscramble))
         {
             _wordToUnscramble = wordToUnscramble;
         }
         else if (!_wordToUnscramble.Equals
              (wordToUnscramble, StringComparison.OrdinalIgnoreCase) && useFiltering)
         {   //If re-using the object and the word is different,
             //we'll need to reload the dictionary
             _dictionaryLoaded = false;
             _wordToUnscramble = wordToUnscramble;
             _results.Clear();
         }
         else if (_wordToUnscramble.Equals
                 (wordToUnscramble, StringComparison.OrdinalIgnoreCase))
         {
             _results.Clear(); //we should clear the results array so they don't stack
         }

         if (!_dictionaryLoaded) //the first call will be slightly slower
             LoadEmbeddedDictionary(wordToUnscramble.ToUpper(), useFiltering);

         string scrambleSorted = SortWord(wordToUnscramble);

         //var kvp = SortedDictionary.FirstOrDefault
         //(p => SortedDictionary.Comparer.Equals(p.Value, scrambledSort));
         var matchList = _sortedDictionary.Where
             (kvp => kvp.Value == scrambleSorted).Select(kvp => kvp.Key).ToList();

         if (matchList.Count > 0)
         {
             foreach (string result in matchList)
             {
                 System.Diagnostics.Debug.WriteLine($"> Match: {result}");
                 _results.Add(result);
             }

             _stopwatch.Stop();
             System.Diagnostics.Debug.WriteLine($"> Elapsed time: {_stopwatch.Elapsed}");
             return _results;
         }
         else //no matches
         {
             _stopwatch.Stop();
             _results.Clear();
             System.Diagnostics.Debug.WriteLine($"> Elapsed time: {_stopwatch.Elapsed}");
             return _results;
         }
     }

     //==================================================================================
     private static void LoadEmbeddedDictionary(string wordText, bool filter = false)
     {
         char[] delims = new char[1] { '\n' };
         string[] chunks;
         int chunkCount = 0;
         if (filter)
             chunks = global::Utility.Properties.Resources.
                                      DictionaryNums.ToUpper().Split(delims);
         else
             chunks = global::Utility.Properties.Resources.
                                      DictionaryNums.ToUpper().Split(delims);

         System.Diagnostics.Debug.WriteLine($"> Length filter: {wordText.Length}");
         _sortedDictionary.Clear();
         foreach (string str in chunks)
         {
             chunkCount++;
             if (filter)
             {
                 //we're assuming the word will have at least 3 characters...
                 //I mean would you really need this program if it was only two?
                 if ((str.Length == wordText.Length) &&
                      str.Contains(wordText.Substring(0, 1)) &&
                      str.Contains(wordText.Substring(1, 1)) &&
                      str.Contains(wordText.Substring(2, 1))) //just checking the 1st,
                                    //2nd & 3rd letter will trim our search considerably
                 {
                     try
                     {
                         _sortedDictionary.Add(str, SortWord(str));
                     }
                     catch
                     {
                         //probably a key collision, just ignore
                     }
                 }
             }
             else
             {
                 try
                 {
                     _sortedDictionary.Add(str, SortWord(str));
                 }
                 catch
                 {
                     //probably a key collision, just ignore
                 }
             }
         }
         System.Diagnostics.Debug.WriteLine($">
         Loaded {_sortedDictionary.Count} possible matches out of {chunkCount.ToString()}");
         _totalEntries = chunkCount;
         _dictionaryLoaded = true;
     }

     //=================================================================================
     private static string SortWord(string str)
     {
         return String.Concat(str.OrderBy(c => c));

         /*** Character Array Method ***
         return String.Concat(str.OrderBy(c => c).ToArray());
         *******************************/

         /*** Traditional Method ***
         char[] chars = input.ToArray();
         Array.Sort(chars);
         return new string(chars);
         ***************************/
     }

     #region [Helper Methods]
     //=================================================================================
     public TimeSpan GetMatchTime()
     {
        return _stopwatch.Elapsed;
     }

     //=================================================================================
     public List<string> GetMatchResults()
     {
        return _results;
     }

     //=================================================================================
     public int GetMatchCount()
     {
        return _results.Count;
     }

     //=================================================================================
     public int GetFilterCount()
     {
        return _sortedDictionary.Count;
     }

     //=================================================================================
     public int GetDictionaryCount()
     {
        return _totalEntries;
     }
     #endregion
}

Testing/Implementation

To drive the code, you would do this...

C#
string scrambled = "mctmouicnaino";
Unscramble obj1 = new Unscramble();
List<string> results = obj1.UnscrambleWord(scrambled);
if (results.Count > 0)
{
    Console.WriteLine($"> Total matches: {obj1.GetMatchCount()}");
    foreach (string str in results)
    {
        Console.WriteLine($">> {str}");
    }
    Console.WriteLine($"> Total time: {obj1.GetMatchTime()}");
    Console.WriteLine($"> Filtered set: {obj1.GetFilterCount()}
                          out of {obj1.GetDictionaryCount()}");
}
else
{
    Console.WriteLine("> No matches available:
            Check your spelling, or the dictionary may be missing this word.");
}

In the class, we could add some more LINQ methods to change the order, take the top result, etc., but this should be a good remedy for any unscramble engine base.

History

  • 11th April, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhere is Utility.Properties.Resources.DictionaryNums ? Pin
largenqcd15-Apr-21 5:33
largenqcd15-Apr-21 5:33 
GeneralRe: Where is Utility.Properties.Resources.DictionaryNums ? Pin
GuildOfCalamity15-Apr-21 19:44
GuildOfCalamity15-Apr-21 19:44 
GeneralRe: Where is Utility.Properties.Resources.DictionaryNums ? Pin
largenqcd16-Apr-21 6:11
largenqcd16-Apr-21 6:11 
GeneralRe: Where is Utility.Properties.Resources.DictionaryNums ? Pin
GuildOfCalamity16-Apr-21 6:41
GuildOfCalamity16-Apr-21 6:41 
QuestionKudos to the author for the high-level heuristic Pin
Martin Fierro14-Apr-21 5:59
Martin Fierro14-Apr-21 5:59 
AnswerRe: Kudos to the author for the high-level heuristic Pin
GuildOfCalamity15-Apr-21 15:13
GuildOfCalamity15-Apr-21 15:13 
GeneralRe: Kudos to the author for the high-level heuristic Pin
Martin Fierro22-Apr-21 6:45
Martin Fierro22-Apr-21 6:45 
QuestionPlease post working code.....working code included. Pin
Member 1088480414-Apr-21 3:47
Member 1088480414-Apr-21 3:47 
AnswerRe: Please post working code.....working code included. Pin
GuildOfCalamity15-Apr-21 15:11
GuildOfCalamity15-Apr-21 15:11 
QuestionNice Approach - Another Variant Pin
Bob McGowan13-Apr-21 17:39
Bob McGowan13-Apr-21 17:39 
AnswerRe: Nice Approach - Another Variant Pin
Member 1341177115-Apr-21 0:01
Member 1341177115-Apr-21 0:01 
QuestionWords with wrong or missing letters from OCR Pin
Curt Krueger13-Apr-21 19:41
Curt Krueger13-Apr-21 19:41 
AnswerRe: Words with wrong or missing letters from OCR Pin
Bob McGowan14-Apr-21 4:21
Bob McGowan14-Apr-21 4:21 
GeneralRe: Words with wrong or missing letters from OCR Pin
Member 1341177115-Apr-21 0:02
Member 1341177115-Apr-21 0:02 
QuestionHow to Unscramble Any Word Pin
Doncp13-Apr-21 11:01
Doncp13-Apr-21 11:01 
AnswerRe: How to Unscramble Any Word Pin
largenqcd13-Apr-21 14:01
largenqcd13-Apr-21 14:01 
AnswerRe: How to Unscramble Any Word Pin
GuildOfCalamity16-Apr-21 5:34
GuildOfCalamity16-Apr-21 5:34 
GeneralRe: How to Unscramble Any Word Pin
largenqcd16-Apr-21 6:32
largenqcd16-Apr-21 6:32 
GeneralRe: How to Unscramble Any Word Pin
GuildOfCalamity16-Apr-21 6:42
GuildOfCalamity16-Apr-21 6:42 
GeneralMessage Closed Pin
14-Nov-21 19:45
kuki yang14-Nov-21 19:45 
AnswerRe: How to Unscramble Any Word Pin
largenqcd16-Apr-21 6:46
largenqcd16-Apr-21 6:46 
GeneralMy vote of 5 Pin
rspercy6513-Apr-21 8:32
rspercy6513-Apr-21 8:32 
GeneralRe: My vote of 5 Pin
GuildOfCalamity15-Apr-21 15:03
GuildOfCalamity15-Apr-21 15:03 
GeneralAnagrams and suggestions in VB Pin
HenkAlles18-Apr-21 23:54
HenkAlles18-Apr-21 23:54 
QuestionAn alternate algorithm Pin
Member 1122079213-Apr-21 8:31
Member 1122079213-Apr-21 8:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.