Search code examples
c#linqcharacterpermutationfactorial

find all possible words of given a list of array with 5 characters


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);
    }
}

Solution

  • Further to the OP clarifying requirements, this new answer provides N lists.

        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 List<char[]> Nchars = new List<char[]>();
    
        static void nestloop(int depth, int width, char[] constructedWord, List<string> words)
        {
            if (depth > 0)
            {
                int i;
                char[] chars = Nchars[depth-1];
                for (i = 0; i < width; i++)
                {
                    constructedWord[depth - 1] = chars[i];
                    
                    nestloop(depth-1, width, constructedWord,words);
                  
                   if (depth==1)
                   {
                        string newWord = "";
                        for (int l = 0; l < constructedWord.Length; l++)
                        {
                            newWord += constructedWord[l];
    
                        }
                        // Remove any spaces
                        newWord = newWord.Replace(" ", "");
                        // Only add the "Word" if it is 2 characters or more
                        if (newWord.Length >= 2)
                        {
                            words.Add(newWord.Replace(" ", ""));
                        }
                    }
                }
            }
        }
        private static List<string> FindWords(int characterDepth)
        {
            List<string> words = new List<string>();
    
            char[] constructedWord = new string('*', characterDepth).ToCharArray();
           
            nestloop(characterDepth, 6, constructedWord, words);
    
            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)
        {
            
            // Add each list of 5 characters here (and a space)
            Nchars.Add(new char[6] { 'A', 'B', 'C', 'D', 'E' ,' '});
            Nchars.Add(new char[6] { 'F', 'G', 'H', 'I', 'J', ' ' });
            Nchars.Add(new char[6] { 'K', 'L', 'M', 'N', 'O', ' ' });
            Nchars.Add(new char[6] { 'P', 'Q', 'R', 'S', 'T', ' ' });
                      
            List<string> allwords = FindWords(Nchars.Count);
            List<string> allwordCombinations = PermuteList(allwords);
        }
    

    This provides the following output depending on the number of character lists used (N) and matches the original posts requiresments for if N=4 then O(5*5*5*5)

    N=2
    O(5*5) 
    allwords = 25
    allwordCombinations = 750
    
    N=3
    O(5*5*5)
    allwords = 125
    allwordCombinations = 750
    
    N=4
    O(5*5*5*5)
    allwords = 625
    allwordCombinations = 15000
    
    N=4 (When also including all 4, 3 and 2 character words)
    O(5*5*5*5)
    allwords = 1275
    allwordCombinations = 18300