13,007,176 members (59,711 online)
Add your own
alternative version

#### Stats

21.7K views
539 downloads
7 bookmarked
Posted 8 Sep 2011

# Really Simple Anagram Solver

, 9 Sep 2011
 Rate this:
Please Sign up or sign in to vote.
One really simple anagram solver using recursion.

## Introduction

I will be back! This is my first post since 5 years ago! If you have been interviewed by Microsoft before, you might have come across this kind of recursive questions. I have written a few different solutions, and this is so far my latest, and maybe the smallest one.

## Background

Always good to get your mind juggled a little bit. If you are in the middle of a Tech Access for a job, hope it can help and good luck!

## Using the Code

If you have VS2010, you will know how to run this project. Here is the recursive method:

```static void GenerateAnagram(IList<string> result, string prefix, string src)
{
if (src.Length == 0)
{
result.Add(prefix);
return;
}

for (int i = 0; i < src.Length; i++ )
{
string tempPrefix = src[i].ToString();
string newSrc = src.Substring(0, i) + src.Substring(i + 1);
var temp = new List<string>();
GenerateAnagram(temp, tempPrefix, newSrc);
foreach (var s in temp)
{
result.Add(prefix + s);
}
}
}```

And here is how to call it:

```var result = new List<string>();
GenerateAnagram(result, "", "ABC");```

And after one of my reader pointed out the memory problem, here is another approach using recursion:

```static IEnumerable<string> GenerateAnagramAlt(string src)
{
if (src.Length == 0) yield break;
if (src.Length == 1) yield return src;

foreach (string rest in GenerateAnagramAlt(src.Substring(1)))
{
for (int i = 0; i < src.Length; i++)
{
string temp = rest.Substring(0, i) + src[0] + rest.Substring(i);
yield return temp;
}
}
}```

I prefer this one as it doesn't have the memory limitation and nearly twice faster when n >= 9 characters. I like its `IEnumberable` syntax as well! I have updated a new project to include the source file. Thanks goes to my friend ajasin1, who came up with this.

## Points of Interest

I have not included logic to filter duplicated cases, as it can be done using LINQ quite easily.

## History

• 08-09-2011: First posted.
• 09-09-2011: 2.0 posted for `GenerateAnagramAlt`.

## License

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

## About the Author

 Software Developer (Senior) www.wonga.com Ireland
I have over 13 Years IT industry experience as Principle/Senior Programmer. I am experienced in .NET/J2EE system design and detailed implementation. I master UML modelling and OO design methodology with a strong C#/C++/Java coding background. I have been in working/managing a distributed project using Agile/Waterfall approach. My business knowledge includes Telecommunication, Financial Investment/Trading, and Banking.

## Comments and Discussions

 First Prev Next
 My vote of 2 FathomSavvy13-Sep-11 7:30 FathomSavvy 13-Sep-11 7:30
 Quickly Out of Memory GenJerDan8-Sep-11 6:05 GenJerDan 8-Sep-11 6:05
 Re: Quickly Out of Memory Ziming8-Sep-11 9:00 Ziming 8-Sep-11 9:00
 Re: Quickly Out of Memory GenJerDan8-Sep-11 9:07 GenJerDan 8-Sep-11 9:07
 Re: Quickly Out of Memory Ziming8-Sep-11 10:03 Ziming 8-Sep-11 10:03
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Jun-17 5:09 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by Ziming
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid