-2

I have a set of characters: "ab", I need every combinations and permutations of it to given length. What I need sets like these if I give the length of 3: a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb

and so on.

I found a sort of good solution (https://stackoverflow.com/a/3640224/18535919), but I think it is a bit complicated. I have found a good solution in the past but I can not find it again.

static int maxlength = 3;
static string ValidChars = "abc";
static void Dive(string prefix, int level)
{
       level += 1;
       foreach (char c in ValidChars)
       {
            Console.WriteLine(prefix + c);
            if (level < maxlength)
            {
                Dive(prefix + c, level);
            }
      }
}
HopeCube
  • 1
  • 1
  • That answer is easy for me to understand. What is your question? – shingo Aug 10 '23 at 09:32
  • i want a simple function that writes out or returns a list bc this answer is not a single Dive("ab", 3) it is a weird thing what my brain couldnot simplify bc im dumb – HopeCube Aug 10 '23 at 09:50
  • This question might be better suited on https://codereview.stackexchange.com/ However if your only problem is the required external variables, why not change the method signature to something like, `static void Dive(string validChars, int maxLength, string prefix = "", int level = 0)`? You can call it externally like `Dive("ab", 3);`, and inside the function it would just be `Dive(validChars, maxLength, prefix + c, level);` – Jeanot Zubler Aug 11 '23 at 07:05
  • @jeanot-zubler that was good idea, it works the way I like it to, thanks – HopeCube Aug 11 '23 at 11:12
  • BTW if i want to put the combinations in a list how sould i do it with this function? – HopeCube Aug 11 '23 at 11:21

2 Answers2

1

Your current code looks simple enough, however if you are looking for generalized approach which doesn't use recursion you can try something like this (frankly speaking it's more complicated, but can be used in general case):

Fiddle

using System.Linq;

...

private static IEnumerable<T[]> SolveMe<T>(int maxLength, IEnumerable<T> chars) {
  if (maxLength < 0)
    throw new ArgumentOutOfRangeException(nameof(maxLength));

  if (chars is null)
    throw new ArgumentNullException(nameof(chars));

  var letters = chars.Distinct().ToArray();

  if (letters.Length == 0)
     yield break;

  for (var agenda = new Queue<T[]>(new[] { Array.Empty<T>() }); 
           agenda.Peek().Length < maxLength;) {
    var current = agenda.Dequeue();

    foreach (var letter in letters) {
      var next = current.Append(letter).ToArray();

      agenda.Enqueue(next);

      yield return next;
    }
  }
}

Demo:

// All solutions as a list
List<char[]> result = SolveMe(3, "ab").ToList();

var report = string.Join(", ", result.Select(item => string.Concat(item)));

Console.Write(report);

Output:

a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You can achieve this using recursive techniques to generate all the combinations and permutations of the given characters. Check the below code:

class Program
{
    static void Main(string[] args)
    {
        string characters = "ab";
        int maxLength = 3;
        var output = GenerateCombinations(characters, maxLength, "");
        Console.WriteLine(string.Join(", ", output.Select(item => string.Concat(item))));
    }

    static IEnumerable<string> GenerateCombinations(string characters, int maxLength, string current)
    {
        if (current.Length > 0)
        {
            yield return current;
        }

        if (current.Length < maxLength)
        {
            foreach (char c in characters)
            {
                foreach (string result in GenerateCombinations(characters, maxLength, current + c))
                {
                    yield return result;
                }
            }
        }
    }
}

Output: a, aa, aaa, aab, ab, aba, abb, b, ba, baa, bab, bb, bba, bbb

Amit Mohanty
  • 387
  • 1
  • 9