// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; class MainClass { private static void FillAndCheck(string s,int ix, int length, char[] set, bool[] used, string target) { if(ix == length) { if(s == target) Console.WriteLine(target + " found"); } else { string tmp; for(int i = 0; i < set.Length; i++) { if(!used[i]) { used[i] = true; tmp = s + set[i]; FillAndCheck(tmp, ix + 1, length, set, used, target); used[i] = false; } } } } private static void FindOneLength(string target, int length, string set) { bool[] used = new bool[set.Length]; for(int j = 0; j < used.Length; j++) used[j] = false; FillAndCheck("", 0, length, set.ToCharArray(), used, target); } public static void Find(string target, int maxlength, string set) { for(int i = 1; i <= maxlength; i++) { FindOneLength(target, i, set); } } public static void Main(string[] args) { Find("bc", 3, "abcd"); Find("xy", 3, "abcd"); Find("d", 3, "abcd"); Find("dcba", 3, "abcd"); } }