-2

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);
    }
}
Mike Mozhaev
  • 2,367
  • 14
  • 13
Amirhossein
  • 39
  • 1
  • 8
  • What have you tried yourself? – Astrid E. Jan 14 '23 at 11:45
  • I think this algorithm should be solved recursively, but the below code only finds all possible words in a board of characters, not for a list of arrays in which each list has 5 characters, and it should take one character from each list, and continue to check all the possible permutations to find words. O(5 * 5 * 5 * 5 * ...n) I really don't know how we should solve recursively. – Amirhossein Jan 14 '23 at 11:59
  • 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);}} – Amirhossein Jan 14 '23 at 11:59
  • This is simply finding permutations in lists. Where's you're main problem? Producing the lists or finding the permutations? The latter problem has many solutions, for example https://stackoverflow.com/q/756055/861716. – Gert Arnold Jan 14 '23 at 13:58
  • Hi Arnold thanks for your message, no, this is different, please read the question again. – Amirhossein Jan 14 '23 at 14:37

3 Answers3

1

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
user20716902
  • 861
  • 1
  • 14
  • Thanks again @user20716902 for your solution, it seems ok but it does not in some of the cases, as you know, we have N lists of arrays, and each array has 5 characters. The algorithm should take one character from each array each time and continue this way for all the possible ways, your algorithm is ok but I don't know why it has not had some results such as "LP","BH","CIO", "ENR" or "JMS", "TIB" in allwordCombinations. I am not sure maybe it should be O(5!*5!*5!...N!) instead of O(5*5*5*...N). – Amirhossein Jan 14 '23 at 17:58
  • @Amirhossein In your previous comment you said you also wanted "LP" ?? but know you say you don't want "LP" ?? Please update you expected results in your questions as it's now not clear what you want. – user20716902 Jan 14 '23 at 18:01
  • Yes, I mean it should show "LP","BH","CIO", "ENR" or "JMS", "TIB", i didn't say I don't want – Amirhossein Jan 14 '23 at 18:06
  • Your algorithms seem very good and interesting, but I don't know why it does not show some results such as "LP","BH","CIO", "ENR" or "JMS", "TIB. I tried allwordCombinations.Contains("JMS") + " " + allwordCombinations.Contains("TIB") but it shows false, – Amirhossein Jan 14 '23 at 18:08
  • All of those letter combinations are in allwordCombinations (I just checked), are you sure that you are using the latest version of this answer? – user20716902 Jan 14 '23 at 18:13
  • @Amirhossein I beleive this answer is working as expected, allwordCombinations.Contains("JMS") shows true – user20716902 Jan 14 '23 at 18:33
  • Let me check your latest code again – Amirhossein Jan 14 '23 at 18:34
  • Wow, You are great, well done, you really surprised me, I really enjoyed your codes! Perfect! – Amirhossein Jan 14 '23 at 18:38
0

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, ...
user20716902
  • 861
  • 1
  • 14
  • Thanks, user20716902, I'll check your code deeply, but please make sure that we have N lists, not only 4 lists. It could be N lists of 5 characters. – Amirhossein Jan 14 '23 at 13:53
  • @Amirhossein, I'll update the example shortly to work with N lists, that actually will make the solution much simpler. – user20716902 Jan 14 '23 at 14:03
  • Hi user20716902, thanks again for your solution, please think about this idea, instead of having static 4 lists, we have dynamic N number lists, that the FindWords function can Handel and find all N character combinations, not a maximum of 4. – Amirhossein Jan 14 '23 at 14:45
  • Hi @Amirhossein, I've provided a new Answer which works as you requested for N lists. – user20716902 Jan 14 '23 at 16:22
  • I also checked allwords.Contains("PL") and allwords.Contains("LP")) and allwordCombinations.Contains("PL") + " " + allwordCombinations.Contains("FL") it gives me false. I think the algorithm should have these also. – Amirhossein Jan 14 '23 at 16:55
  • When N is set to 2 only the first 2 lists would be used, are you now saying that with N==4 you also want all 4 letters, 3 letter and 2 letter words returned also? In your own example P and L only appear in characters3 and characters4 – user20716902 Jan 14 '23 at 17:03
  • Yes, it should show all the possible paths, when N is 4 for example it should show two digits and also even vice versa. – Amirhossein Jan 14 '23 at 17:37
-1

We can use loops. Each character is going to be mentioned in each possible order, but then we need to manually put each character in a different order, so the code becomes 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!

kibrjr BC
  • 86
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '23 at 16:35