-4

How do I go about getting a combination and variation of an array of strings? let's say A, B, C, D expected to get something like this, which able to compute out even AB and BA view as differents combination.

A
AB
AC
ABC
ABCD
ACBD
...

B
BA
BC
BCD
BADC
...

Edit:

currently what i try was like code below by using List, was thinking should i make another permutation(like set= abcd,bcda,cdab.....) for my string set in order to get the full list?

    string set = "abcd";

    // Init list
    List<string> subsets = new List<string>();

    // Loop over individual elements
    for (int i = 1; i < set.Length; i++)
    {
        subsets.Add(set[i - 1].ToString());

        List<string> newSubsets = new List<string>();

        // Loop over existing subsets
        for (int j = 0; j < subsets.Count; j++)
        {
            string newSubset = subsets[j] + set[i];
            newSubsets.Add(newSubset);
        }

        subsets.AddRange(newSubsets);
    }

    // Add in the last element
    subsets.Add(set[set.Length - 1].ToString());
    subsets.Sort();
vzwick
  • 11,008
  • 5
  • 43
  • 63
Lucas
  • 11
  • 1
  • 1
    Have you tried anything? You can't just post requirements with no effort and expect help or someone to do your work for you. – tnw Feb 26 '16 at 16:01
  • Eric Lippert has blog posts about producing [combinations](http://ericlippert.com/2014/10/13/producing-combinations-part-one/) and [permutations](http://ericlippert.com/2013/04/15/producing-permutations-part-one/) that you might want to read. – juharr Feb 26 '16 at 16:04
  • i did go through many different threads, like http://stackoverflow.com/questions/756055/listing-all-permutations-of-a-string-integer which is very good but still cant fulfill my need, as i need also the subset of the string and like AB and BA have to views as different string. – Lucas Feb 26 '16 at 16:04
  • You could use this regex: ^(?:([A-D])(?!.*\1)){1,4}$ – Abyte0 Feb 26 '16 at 16:06

1 Answers1

0

As I found it difficult to understand your question I apologize for my limited response. However I will give it a shot. I attempted to make a program in C which will display combinations of an array of characters which is similar to a string, just without a '\0'. I will attempt to convert my code to C#.

/* String for combos to be enumerated */
char[] set = { 'a', 'b', 'c', 'd' };
/* Length of the array */
int setLength = set.Length, a, b, c;

/* This will print all combos that have 1 value. E.g. A, B, C, D */
for (a = 0; a < setLength; a++) 
{
    Console.WriteLine(set[a] + Environment.NewLine);
}

/* This will give the 1st value of the combo */
for (a = 0; a < setLength; a++)
{
    /* This will give the 2nd value. Resulting in combos with a length of 2 */
    for (b = 0; b < setLength; b++)         
    {
        Console.WriteLine(set[a] + set[b] + Environment.NewLine);
    }
}

/* 1st value */
for (a = 0; a < setLength; a++)
{
    /* 2nd value */
    for (b = 0; b < setLength; b++)
    {
        /* 3rd value */
        for (c = 0; c < setLength; c++)
        {
            Console.WriteLine(set[a] + set[b] + set[c] + Environment.NewLine);
        }
    }
}
/* To continue with longer combos simply add more and more for loops */

Output: Output will be different as I changed it to fit in a single screenshot

Output will be different as I changed it to fit in a single screenshot I hope this helped.

EDIT: I found a similar question that had already been answered: Listing permutations of different combination of characters Please look this over as it covers very similar topics in this question.

Community
  • 1
  • 1