user63641
user63641

Reputation:

Need recursive function for generating unique combination of strings

I have some(say 9, no not definite) unique strings from database(A,B,C,D,E,F,G,H) and I want to create unique combination of these fields to populate the listbox so that user can select single or different combination of these string fields like A,B,C,D,E,F,G,H, AB,AC,AD,AE,AF,AG,AH,AC,AD,AE,AF,AG,AG,AH,... ABC,ABD,ABE,ABF,ABG,ABH,ACD,ACE,ACF,ACG,ACH,.....

In C# (win application) Thanks in advance

Upvotes: 2

Views: 2224

Answers (3)

bhuang3
bhuang3

Reputation: 3633

public class Combination { static int count = 0;

public static void main(String[] args)
{
    StringBuilder out = new StringBuilder("");
    StringBuilder str = new StringBuilder("aabcbd");

    combination(str, out, 0);
    System.out.println("The Count : " + count);
}


// Recursive
static void combination(StringBuilder in, StringBuilder out, int start)
{
    int len = in.length();

    for (int i = start; i < len; i++)
    {
        if (isAppended(in, out, i))
        {
            continue;
        }

        out.append(in.charAt(i));
        count++;
        System.out.println(out);
        combination(in, out, i + 1);
        out.deleteCharAt(out.length() - 1);
    }
}


static boolean isAppended(StringBuilder in, StringBuilder out, int index)
{
    int inCount = 0;
    int outCount = 0;

    int i = 0;
    int len = out.length();
    char ch = in.charAt(index);

    for (i = 0; i < index; i++)
    {
        if (in.charAt(i) == ch)
        {
            inCount++;
        }
    }

    for (i = 0; i < len; i++)
    {
        if (out.charAt(i) == ch)
        {
            outCount++;
        }
    }

    if (inCount != outCount)
    {
        return true;
    }

    return false;
}

}

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062502

My first choice would be "use a CheckedListBox and let the user make their picks themselves" - this will save their sanity... would you want to look for "ABCEFH" in a long list?).

If you want the strings: How about just using binary arithmetic? i.e. use a number (bit-length as per the number of elements), and simply keep incrementing, including the set bits each time? So in C#, for example:

static void Main()
{
    foreach (string value in GetCombinations(
        "A", "B", "C", "D", "E", "F", "G", "H"))
    {
        Console.WriteLine(value);
    }
}
static IEnumerable<string> GetCombinations(params string[] tokens)
{
    ulong max = (ulong) 1 << tokens.Length;
    StringBuilder builder = new StringBuilder();
    // test all bitwise combinations
    for (ulong value = 0; value < max; value++)
    {
        builder.Length = 0;
        ulong tmp = value;
        // include the tokens for the set bits
        for (int i = 0; i < tokens.Length; i++)
        {
            if ((tmp & (ulong)1) == 1) builder.Append(tokens[i]);
            tmp >>= 1;
        }
        yield return builder.ToString();
    }
}

If you want the data in the order per your example, LINQ is helpful:

    foreach (string value in GetCombinations(
          "A", "B", "C", "D", "E", "F", "G", "H")
        .OrderBy(s=>s.Length)
        .ThenBy(s=>s))
    {
        Console.WriteLine(value);
    }

Upvotes: 8

user414441
user414441

Reputation:

This is like counting integers in base 'n' .. should not be difficult.

Upvotes: 0

Related Questions