using System.Collections.Generic;
using System.Threading.Tasks;
namespace BruteForceAlgorithm
public delegate bool BruteForceTest(ref char[] testChars);
public static bool BruteForce(string testChars, int startLength, int endLength, BruteForceTest testCallback)
for (int len = startLength; len <= endLength; ++len)
char[] chars = new char[len];
for (int i = 0; i < len; ++i)
if (testCallback(ref chars))
for (int i1 = len - 1; i1 > -1; --i1)
for (i2 = testChars.IndexOf(chars[i1]) + 1; i2 < testChars.Length; ++i2)
chars[i1] = testChars[i2];
if (testCallback(ref chars))
for (int i3 = i1 + 1; i3 < len; ++i3)
if (chars[i3] != testChars[testChars.Length - 1])
if (i2 == testChars.Length)
chars[i1] = testChars[0];
static void Main(string[] args)
BruteForceTest testCallback = delegate(ref char[] testChars)
var str = new string(testChars);
bool result = BruteForce("abcde", 1, 5, testCallback);
Console.WriteLine(result);