|
-
Mar 2nd, 2006, 09:38 PM
#1
Thread Starter
Hyperactive Member
Generating ALL possible combinations
Hi everybody,
I have an array of letter for example:
VB Code:
string strings[] = { "a", "b", "c", "d", "e"};
I want to generate all possible combinations of a specific length which is determined by the user, for example when he selects 3:
aaa
aab
aac
aad
aae
aba
abb
abc
abd
abe
abd
aca
acb
acc
acd
ace
etc...
I started by this code but it's limited to the length I assume before the runtime
VB Code:
foreach (string l1 in strings)
{
foreach (string l2 in strings)
{
foreach (string l3 in strings)
{
myword = l1 + l2 + l3;
}
}
}
can you think of any solution that works with any length selected by the user at runtime?
Thanks
-
Mar 2nd, 2006, 10:30 PM
#2
Re: Generating ALL possible combinations
A generic algorithm would use three loops. You'd create a StringCollection that contained all possible one-letter combinations. You'd then have an outer for loop that iterated from 2 to the length of the fiianl strings you want, which is 3 in the example you provided. You then have an inner For Each loop that iterated over all the items of your current StringCollection, then iside that another For Each loop that iterated over all the characters in your array. You would then create a new StringCollection inside that third loop by concatenating the character to the current string and adding it to the new collection. That new collection now becomes your existing collection and you start a new iteration of the outer loop. I'm guessing homework of some kind so I'll leave the implementation up to you.
-
Mar 2nd, 2006, 10:37 PM
#3
Re: Generating ALL possible combinations
Here's a start:
Code:
int finalLength = 3; // The length of the final strings.
string[] characters = new string[] { "a", "b", "c", "d", "e" };
System.Collections.Specialized.StringCollection existingStrings;
System.Collections.Specialized.StringCollection newStrings;
// Start with the all one-letter combinations.
newStrings.AddRange(characters);
for (int i = 2; i <= finalLength; i++)
{
// Start a new collection of strings based on the existing strings.
existingStrings = newStrings;
newStrings = new System.Collections.Specialized.StringCollection();
// Put the other loops here.
}
-
Mar 2nd, 2006, 10:59 PM
#4
Thread Starter
Hyperactive Member
Re: Generating ALL possible combinations
Hi
First of all, thanks for your help.
Actually, it's not a homework but I am trying to make a bruteforce tool for a hashing function
I didn't understand your approach, for example what if I want to generate all 5-letter strings.
It would be great if you could kindly complete your code, it will save alot of time 
Thanks again
-
Mar 2nd, 2006, 11:26 PM
#5
Re: Generating ALL possible combinations
Code:
int finalLength = 3; // The length of the final strings.
string[] characters = new string[] { "a", "b", "c", "d", "e" };
System.Collections.Specialized.StringCollection existingStrings;
System.Collections.Specialized.StringCollection newStrings = new System.Collections.Specialized.StringCollection(); // Start with the all one-letter combinations.
newStrings.AddRange(characters);
for (int len = 2; len <= finalLength; len++)
{
// Start a new collection of strings based on the existing strings.
existingStrings = newStrings;
newStrings = new System.Collections.Specialized.StringCollection();
// Concatenate every string of length (len-1)...
foreach (string str in existingStrings)
{
// ...with every character...
foreach (string ch in characters)
{
// ...to create every possible string of length len.
newStrings.Add(str + ch);
}
}
}
All you need to do is change the value of 'finalLength' to however many characters you want in all the final strings. You could also accomplish this task using recursion if you wanted as it's a prime candidate.
-
Mar 2nd, 2006, 11:50 PM
#6
Thread Starter
Hyperactive Member
Re: Generating ALL possible combinations
Great, thanks man it works like a charm
Which approach is better if we take performance and speed into consideration?
Also how can I use concatenated strings in the most inner loop (i.e 3rd loop) so that I can directly compare the generated string with a certain string without implementing another loop that iterates over newStrings collection
Regards
-
Mar 2nd, 2006, 11:57 PM
#7
Re: Generating ALL possible combinations
Recursion is often easier to write, as long as you understand the principle properly, but iteration will generally be more efficient. That's unless you have to tie yourself in knots to achieve it using interation.
Collections have a Contains method. If you're using an array there is Array.IndexOf.
-
Mar 3rd, 2006, 12:11 AM
#8
Thread Starter
Hyperactive Member
Re: Generating ALL possible combinations
Contains will work if I want to find something in the original collection
but I don't want that, I want for example to encrypt the string and then to print it
I tried something like this inside the most inner loop but I got index out of range exception
Code:
int w=0;
....
....
// ...to create every possible string of length len.
newStrings.Add(str + ch);
Console.WriteLine(newStrings[w]);
w++;
-
Mar 3rd, 2006, 12:13 AM
#9
Re: Generating ALL possible combinations
I don't understand what you're trying to achieve with that. If you want to get the last string in the collection then just use newStrings[newStrings.Count - 1]. Otherwise you're going to have to explain your aim.
-
Mar 3rd, 2006, 12:17 AM
#10
Thread Starter
Hyperactive Member
Re: Generating ALL possible combinations
I want to print all generated strings, I think it's clear now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|