Sign in to follow this  
softengg

[.net] letter combinations

Recommended Posts

Hi, I am using C#.Net 2 WinApps...pls tell how can i find out the different possible letter combinations in a word?. Supppose that the different possible letter combination of the word "eat" will be like this : aet,tea,eat,tae,ate, aet, etc.....how programatically find this......

Share this post


Link to post
Share on other sites
Well, the overly complicated method is to either systematically scramble the word or to recursively do it. Now, I'm using C5 for a collection class because it has sets which do the deduplication, but you can simulate the functionality with the normal collections--I'm just not going to tell you how to do that, it should be fairly obvious.

using C5;
using System;

public class Entry
{
public static void Main(string [] args)
{
Console.WriteLine("Starting...");
Console.WriteLine("cat: {0}", String.Join(", ", GetCombinations("cat")));
Console.WriteLine("dmoonfire: {0}", String.Join(", ", GetCombinations("dmoonfire")));
}

public static string[] GetCombinations(string name)
{
// Create a hash set to keep a unique list. You can use this
// with generics or System.Collections, but I like C5.
HashSet<string> combinations = new HashSet<string>();

// Get the combinations of every letter
GetCombinations(combinations, "", name);

// Return the results
return combinations.ToArray();
}

private static void GetCombinations(HashSet<string> combinations, string prefix, string input)
{
// If we only have one letter, just add it and return
if (input.Length == 1)
combinations.Add(prefix + input);

// Otherwise, we go through the entire list and pull out one
// letter at a time, then recursively look through the rest
for (int i = 0; i < input.Length; i++)
{
// Build a new prefix with the new letter
string newPrefix = prefix + input[i];

// Pull out the stuff before and after it
string before = input.Substring(0, i);
string after = input.Substring(i + 1);

// Recursion is your friend
GetCombinations(combinations, newPrefix, before + after);
}
}
}

Share this post


Link to post
Share on other sites
I must assume everyone is being honest. :) Plus, if it was a homework question, then the only person that is going to get hurt is the person cheating.

I remember some years back when I was trying to write this exact function. For some reason, I had a brain fart, so I tried searching for it and never found anything useful. So, I puzzled through it. Since I'm planning on using this for a game (someday, gotta finish CuteGod first), I figured it was easy enough to just throw it together.

Share this post


Link to post
Share on other sites
Quote:

Now, I'm using C5 for a collection class

Totally offtopic, but C5 was awesome until they refused to fix their event serialization bugs. :( Still a really solid library other than that, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

Now, I'm using C5 for a collection class

Totally offtopic, but C5 was awesome until they refused to fix their event serialization bugs. :( Still a really solid library other than that, though.


Really? I've never had a problem with any request I had from the C5 folks. I was doing Debian packages for it and wanted a .snk file so I didn't have to make a Debian-specific one. Got an email just a bit later with it. The main problem, as I see it, is that it was basically for a school project that kind of drifted after that. They haven't done much in versions and I know the C5 packaged in with Mono is rather dated since C5 won't compile under Mono yet (or at least when I last tried it) but it works beautifully with Mono.

Of course, I never tried to really serialize stuff using the built-in since I have so much trouble with interfaces that it simply wasn't worth it. Most of the non-trivial cases of my code always end up having custom serializers.

But, I really like C5 for my stuff. My C# libraries pretty much use C5 exclusively, including the sprite engine. Then again, I'm rather fond of the lowly set.

Share this post


Link to post
Share on other sites
Hi, Thanks for the code..it works fine........How can we find possible combinations of two different arrays?? Suppose we have 2 diff arrays such as

Array1 = {"I","O","RS"}
Array2 = {"AB","D","CVB","M"}

And the combination will be...IAB, ABD, ABDCVB, ABDCVBI, MRS, RSM, SRM, OBA, etc.... Any idea abt it.........

I am using C#.Net 2 win apps....

Thanks in advance................

Share this post


Link to post
Share on other sites
Dude, this is not a "please write my code for me" forum. If you have specific questions about specific problems you are having, we will be glad to help you out. We are not here to give you code.

Why don't you try to write it yourself and then, if you have problems, post what the problems are and what you've done to try to resolve them. Then you will learn something and we'll be more inclined to help you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this