-1

Let's say I have an array like this one:

string[] separatingStrings = {"Hello", "Hi"};

I want to get all these results in another array:

result[0] = Hi
result[1] = Hello
result[2] = Hi Hello
result[3] = Hello Hi

As you can see, those are 4 results. If I entered three elements, I would get a total of 15 results.

Example with a string array: {"11", "22", "33"}

result[0] = 11
result[1] = 22
result[2] = 33
result[3] = 11, 22
result[4] = 11, 33
result[5] = 22, 11
result[6] = 22, 33
result[7] = 33, 11
result[8] = 33, 22
result[9] = 11, 22, 33
result[10] = 11, 33, 22
result[11] = 22, 11, 33
result[12] = 22, 33, 11
// And so on...

It's not necessary to have that order, I just want every possible variation (without repeating the same value on the index) on another array).

I've tried to do it myself and failed. I tried searching over internet but everyone was using integer variables or splitting the strings into chars.

janw
  • 8,758
  • 11
  • 40
  • 62
Frankkez
  • 7
  • 2
  • Can the input array contain duplicate values? For instance, if the input array is `11, 11, 11`, is the total result then `11` - `11, 11` - `11, 11, 11` ? – Lasse V. Karlsen Jun 22 '20 at 13:10
  • @LasseV.Karlsen Nope it needs to be without repeating – Frankkez Jun 22 '20 at 13:13
  • So is the input array invalid? Or do you mean you would simply want to see `11` as the output then? – Lasse V. Karlsen Jun 22 '20 at 13:36
  • @LasseV.Karlsen umm yeah i just want to make all those "combinations" get together in a string, using string.join and maybe separating everything with a #. %%% Short example: string[] separatingStrings = {"11","22"}; has to give me the result of another array having everything like this: {"11", "22", "11 22", "22 11"} if you didn't understand what i mean please tell me, thanks for being here and helping – Frankkez Jun 22 '20 at 13:46
  • I've modified the post a bit so it's easier to understand – Frankkez Jun 22 '20 at 13:52

2 Answers2

0

Have you tried assigning every string to a specific integer and then do the same procedure as if you're working with integers?

dreevo
  • 19
  • 8
  • I dont speak english so if i got you wrong i'm sorry, but if you mean that if i tried to convert all those functions using integers to use strings, i tried yeah, but they would give errors and the software wouldn't compile. – Frankkez Jun 22 '20 at 13:07
  • Haha i deleted it because of anxiety, let me do CTRL + Z – Frankkez Jun 22 '20 at 13:11
  • This is one, it works but shows only permutations, what means it wont show 11, then 22 and then 33, but it would only make the permutations the same size as the amount of elements given. – Frankkez Jun 22 '20 at 13:12
  • public static IEnumerable Permutations(int start, int count) { if (count == 0) yield break; var array = Enumerable.Range(start, count) .ToArray(); if (count > 1) { do yield return array; while (NextPermutation(ref array)); } else yield return array; } (FIRST PART) – Frankkez Jun 22 '20 at 13:12
  • private static bool NextPermutation(ref int[] array) { int k = array.Length - 2; while (k >= 0) { if (array[k] < array[k + 1]) break; k--; } – Frankkez Jun 22 '20 at 13:12
  • if (k < 0) return false; int l = array.Length - 1; while (l > k) { if (array[k] < array[l]) break; l--; } int tmp = array[k]; array[k] = array[l]; array[l] = tmp; Array.Reverse(array, k + 1, array.Length - k - 1); return true; } – Frankkez Jun 22 '20 at 13:13
  • sorry for posting it like that, it wouldn't let me do it all at one – Frankkez Jun 22 '20 at 13:13
  • This was the source: https://stackoverflow.com/questions/31201251/get-all-possible-combinations-from-a-list-of-string – Frankkez Jun 22 '20 at 13:15
-1

As a start, note that the longer lists contain the shorter lists inside.

I.e., lists of length 2 contain the lists of length 1 and add one element, lists of length 3 contain the lists of length 2, etc.

janw
  • 8,758
  • 11
  • 40
  • 62
P. Jindra
  • 47
  • 1
  • 8
  • If it was a homework problem i would preferr to ask to my professor to help me, i wouldn't do it here. If i didn't understand you incorrectly, i already did that but i made it wrong, i was getting "a, ab, a, ab" instead of "a, ab, b, ba", which i tried to fix by making it use two index, one for the letter at the left and another to the letter on the right. Thing is it would solve that but not when there were 3 elements. I tried all the morning to get a solution and couldn't, so i came here to ask help. – Frankkez Jun 22 '20 at 13:05