I need to create all the possible words of given a list of array with 5 characters which means all possible permutate O(5*5... n).
For example, we have n as 4, so we have 4 lists which each list have 5 characters, I want to find all possible words from these characters.
N = 4
char[] characters1 = new char[5] { 'A', 'B', 'C', 'D', 'E' };
char[] characters2 = new char[5] { 'F', 'G', 'H', 'I', 'J' };
char[] characters3 = new char[5] { 'K', 'L', 'M', 'N', 'O' };
char[] characters4 = new char[5] { 'P', 'Q', 'R', 'S', 'T' };
So, we have 4 lists, and each list has 5 characters, it should take one character from each list and continue to check all the possible permutations to find words. O(5 * 5 * 5 * 5)
For example, it should take one character and check all the possible ways
AF,AG,AH,AI,AJ,BF,BG,BH,BI,BJ,...FB,FC,FD,FE,GA,GB,GC,GD,GE,... (Checking for words with 2 characters ) AFK,AFL,AFM,AFN,AFO,BFK,BFL,...FBK,FBL,FBM,FBN,FBO,... (Checking for words with 3 characters) AFKP,AFKQ,AFKQ,.... (Checking for words with 4 characters) `
char[] characters1 = new char[5] { 'A', 'B', 'C', 'D', 'E' };
char[] characters2 = new char[5] { 'F', 'G', 'H', 'I', 'J' };
char[] characters3 = new char[5] { 'K', 'L', 'M', 'N', 'O' };
char[] characters4 = new char[5] { 'P', 'Q', 'R', 'S', 'T' };
Private List<string> FindWords(List<char[]> characters) //
{
// length of arrays are always 5
}
static void permute(String s, String answer)
{
if (s.Length == 0)
{
Console.Write(answer + " ");
return;
}
for (int i = 0; i < s.Length; i++)
{
char ch = s[i];
String left_substr = s.Substring(0, i);
String right_substr = s.Substring(i + 1);
String rest = left_substr + right_substr;
permute(rest, answer + ch);
}
}