2

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

1
  • Given that you are preserving order, you will have 2**9 = 512 possible strings generated. This will seriously test the limits of your users' patience if you put all of these in a listbox for them to pick from. Commented Sep 23, 2009 at 13:01

3 Answers 3

8

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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

}

Comments

0

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

1 Comment

But I need Unique combination and not double(AA,BB) or repeated combinations(AB,BA)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.