I am giving input as int and according to that input I want combination of two characters,
FOR EXAMPLE
I am giving input as 2 and I have two characters x and y so I want combinations like
xx,yy,xy,yx
If Input is 3,I want
xxx,xyy,xxy,xyx,yxx,yyy,yxy.yyx
and so on,I have try with following code,
int input1 = 4;
Double totalpossibilities = Math.Pow(2, input1);
string[] PArray = new string[Convert.ToInt16(totalpossibilities)];
char[] chars = new char[] { 'x', 'y'};
for (int i = 0; i < totalpossibilities; i++)
{
string possibility = "" ;
for (int j = 0; j < input1; j++)
{
Random random = new Random();
int r = random.Next(chars.Length);
char randomChar = chars[r];
possibility = possibility + randomChar;
}
if (PArray.Contains(possibility))
{
i--;
}
else
PArray[i] = possibility;
}
But as you can see I am using random function So I takes too long to complete,Is there any different logic?
You could run a for loop from 0 to totalpossibilities.
Convert i to binary, for example, at iteration 20 this would result in “10100”.
Pad the result to input1 characters, for example (for 8 places): 00010100
Then convert to a string and replace all zeroes with “x”, all ones with “y”.