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);}}
CodePudding user response:
Here's the solution for you, the example below first fetches all words based on the character arrays that you provided, character1 character2 etc...
The the PermuteList method finds all character combinations of those words.
FindWords method accepts the number of characters you are fetching, below I've shown the usage for 2 characters, just change to FindWords(3) to find all 3 character combinations etc...
private static void permuteA(String str,
int l, int r, List<string> words)
{
if (l == r)
words.Add(str);
else
{
for (int i = l; i <= r; i )
{
str = swap(str, l, i);
permuteA(str, l 1, r, words);
str = swap(str, l, i);
}
}
}
public static String swap(String a,
int i, int j)
{
char temp;
char[] charArray = a.ToCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
string s = new string(charArray);
return s;
}
static char[] characters1 = new char[5] { 'A', 'B', 'C', 'D', 'E' };
static char[] characters2 = new char[5] { 'F', 'G', 'H', 'I', 'J' };
static char[] characters3 = new char[5] { 'K', 'L', 'M', 'N', 'O' };
static char[] characters4 = new char[5] { 'P', 'Q', 'R', 'S', 'T' };
private static List<string> FindWords(int characterDepth)
{
List<string> words = new List<string>();
string constructedWord = "";
for (int i = 0; i < characters1.Length; i )
{
char char1 = characters1[i];
for (int j = 0; j < characters2.Length; j )
{
char char2 = characters2[j];
if (characterDepth == 2)
{
words.Add(char1.ToString() char2.ToString());
continue;
}
for (int k = 0; k < characters3.Length; k )
{
char char3 = characters3[k];
if (characterDepth == 3)
{
words.Add(char1.ToString() char2.ToString() char3.ToString());
continue;
}
for (int l = 0; l < characters4.Length; l )
{
char char4 = characters4[l];
words.Add(char1.ToString() char2.ToString() char3.ToString() char4.ToString());
}
}
}
}
return words;
}
private static List<string> PermuteList(List<string> startWords)
{
List<string> newwords = new List<string>();
for (int w = 0; w < startWords.Count; w )
{
permuteA(startWords[w], 0, startWords[w].Length-1, newwords);
}
return newwords;
}
static void Main(string[] args)
{
List<string> allwords = FindWords(2);
List<string> allwordCombinations = PermuteList(allwords);
}
Outputof the resulting lists will then be:
allwords = AF, BF, CF, DF, EF, ...
After finding all permutations:
allwordCombinations = AF, FA, BF, FB, CF, FC, DF, FC, EF, FE, ...
CodePudding user response:
We can use loops for each character be mentioned in each possible order, but then we need to manualy put each character in an order, so the code becames this:
private List<string> FindWords(List<char[]> characters)
{
List<string> words = new List<string>();
for (int i = 0; i < characters1.Length; i )
{
char char1 = characters1[i];
for(int j = 0; j < characters2.Length; j )
{
char char2 = characters2[j];
for (int k = 0; k < characters3.Length; k )
{
char char3 = characters3[k];
for (int l = 0; l < characters4.Length; l )
{
char char4 = characters4[l];
// 2 characters
words.Add(char1.ToString() char2.ToString());
words.Add(char1.ToString() char3.ToString());
words.Add(char1.ToString() char4.ToString());
words.Add(char2.ToString() char1.ToString());
words.Add(char2.ToString() char3.ToString());
words.Add(char2.ToString() char4.ToString());
words.Add(char3.ToString() char1.ToString());
words.Add(char3.ToString() char2.ToString());
words.Add(char3.ToString() char4.ToString());
words.Add(char4.ToString() char1.ToString());
words.Add(char4.ToString() char2.ToString());
words.Add(char4.ToString() char3.ToString());
// 3 characters
words.Add(char1.ToString() char2.ToString() char3.ToString());
words.Add(char1.ToString() char2.ToString() char4.ToString());
words.Add(char1.ToString() char3.ToString() char2.ToString());
words.Add(char1.ToString() char3.ToString() char4.ToString());
words.Add(char1.ToString() char4.ToString() char2.ToString());
words.Add(char1.ToString() char4.ToString() char3.ToString());
words.Add(char2.ToString() char1.ToString() char3.ToString());
words.Add(char2.ToString() char1.ToString() char4.ToString());
words.Add(char2.ToString() char3.ToString() char1.ToString());
words.Add(char2.ToString() char3.ToString() char4.ToString());
words.Add(char2.ToString() char4.ToString() char1.ToString());
words.Add(char2.ToString() char4.ToString() char3.ToString());
words.Add(char3.ToString() char1.ToString() char2.ToString());
words.Add(char3.ToString() char1.ToString() char4.ToString());
words.Add(char3.ToString() char2.ToString() char1.ToString());
words.Add(char3.ToString() char2.ToString() char4.ToString());
words.Add(char3.ToString() char4.ToString() char1.ToString());
words.Add(char3.ToString() char4.ToString() char2.ToString());
words.Add(char4.ToString() char1.ToString() char3.ToString());
words.Add(char4.ToString() char1.ToString() char2.ToString());
words.Add(char4.ToString() char3.ToString() char1.ToString());
words.Add(char4.ToString() char3.ToString() char2.ToString());
words.Add(char4.ToString() char2.ToString() char1.ToString());
words.Add(char4.ToString() char2.ToString() char3.ToString());
// 4 characters
words.Add(char1.ToString() char2.ToString() char3.ToString() char4.ToString());
words.Add(char1.ToString() char2.ToString() char4.ToString() char3.ToString());
words.Add(char1.ToString() char3.ToString() char2.ToString() char4.ToString());
words.Add(char1.ToString() char3.ToString() char4.ToString() char2.ToString());
words.Add(char1.ToString() char4.ToString() char3.ToString() char2.ToString());
words.Add(char1.ToString() char4.ToString() char2.ToString() char3.ToString());
words.Add(char2.ToString() char4.ToString() char3.ToString() char1.ToString());
words.Add(char2.ToString() char4.ToString() char1.ToString() char3.ToString());
words.Add(char2.ToString() char3.ToString() char4.ToString() char1.ToString());
words.Add(char2.ToString() char3.ToString() char1.ToString() char4.ToString());
words.Add(char2.ToString() char1.ToString() char3.ToString() char4.ToString());
words.Add(char2.ToString() char1.ToString() char4.ToString() char3.ToString());
words.Add(char3.ToString() char1.ToString() char2.ToString() char4.ToString());
words.Add(char3.ToString() char1.ToString() char4.ToString() char2.ToString());
words.Add(char3.ToString() char2.ToString() char1.ToString() char4.ToString());
words.Add(char3.ToString() char2.ToString() char4.ToString() char1.ToString());
words.Add(char3.ToString() char4.ToString() char1.ToString() char2.ToString());
words.Add(char3.ToString() char4.ToString() char2.ToString() char1.ToString());
words.Add(char4.ToString() char1.ToString() char2.ToString() char3.ToString());
words.Add(char4.ToString() char1.ToString() char3.ToString() char2.ToString());
words.Add(char4.ToString() char2.ToString() char1.ToString() char3.ToString());
words.Add(char4.ToString() char2.ToString() char3.ToString() char1.ToString());
words.Add(char4.ToString() char3.ToString() char2.ToString() char1.ToString());
words.Add(char4.ToString() char3.ToString() char1.ToString() char2.ToString());
}
}
}
}
return words;
}
I think this code it's going to work for you!