Results 1 to 10 of 10

Thread: Generating ALL possible combinations

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    Generating ALL possible combinations

    Hi everybody,

    I have an array of letter for example:
    VB Code:
    1. 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:
    1. foreach (string l1 in strings)
    2.             {
    3.                 foreach (string l2 in strings)
    4.                 {
    5.                     foreach (string l3 in strings)
    6.                     {
    7.                         myword = l1 + l2 + l3;
    8.                     }
    9.                 }
    10.             }


    can you think of any solution that works with any length selected by the user at runtime?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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++;

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2001
    Location
    Earth
    Posts
    277

    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
  •  



Click Here to Expand Forum to Full Width