bool stringsRearrangement(string[] inputArray) { IEnumerable<IEnumerable<int>> permutations = GetPermutations(Enumerable.Range(1, inputArray.Length), inputArray.Length); Dictionary<int, bool> dictionary = new Dictionary<int, bool>(); int key = 0; foreach (IEnumerable<int> intArray in permutations) { key++; List<int> intList = intArray.ToList(); for (int i = 0; i < intList.Count - 1; i++) { int diffcount = 0; for (int j = 0; j < inputArray[intList[i] - 1].Length; j++) if (inputArray[intList[i] - 1][j] != inputArray[intList[i + 1] - 1][j]) diffcount++; if (diffcount != 1) { dictionary.Add(key, false); break; } } if (!dictionary.ContainsKey(key)) dictionary.Add(key, true); } return dictionary.Any(x => x.Value); } IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length) { if (length == 1) return list.Select(t => (IEnumerable<T>)new T[] { t }); return GetPermutations(list, length - 1) .SelectMany(t => list.Where(e => !t.Contains(e)), (t1, t2) => t1.Concat(new T[] { t2 })); }
Previous Next