TrickyMood
TrickyMood

Reputation: 53

How to generate all possible words

I'm new to programming (C#).

The application is a 'words generator'.

What I'm looking for is a for loop that can generate all possible words with the characters in a given array of characters.

The details:

I have a List<char> = { A,a,6,w,# } (The length may vary)

I want to generate all possible words (for example: 4 letters length ) with this character set. This options should generate 5 characters & 4 letters = 5*5*5*5 = 625 words.

All generated words should be every possible combination of the given letters only

NOTE: Some might tell me that i should use a solution called (Permutations of a String/Integer) this method seems to be fine if the required words length is same as given characters length, but in my case i might give the application 100 characters, But i want it to generate all possible words -> 4 letters length (Example: MaRk, M@rK,m4rK...)

Upvotes: 2

Views: 5047

Answers (2)

Willem Van Onsem, thanks! This was exactly what i've been looking for. But my problem sounds little different. I have to generate all possible strings without repetition of chars from source array. And here is your code, that i modified to do so:

    public static IEnumerable<string> GenerateStrings(IEnumerable<char> characters, int length, int count)
    {
        if (length > 0)
        {
            foreach (char c in characters)
            {
                char[] charactersDec = new char[characters.Count()];
                Array.Copy(characters.ToArray(), charactersDec, characters.Count());
                int index = Array.IndexOf(charactersDec, c);
                charactersDec = charactersDec.Where((val, idx) => idx != index).ToArray();
                foreach (string suffix in GenerateStrings(charactersDec, length - 1, count++))
                {
                    yield return c + suffix;
                }
            }
        }
        else
        {
            yield return string.Empty;
        }
    }

I remove current char from array and passed this array to recursive call.

output for a, b, c, d will be: ab ba ac ca ad da bc cb bd db cd dc please, sorry my english.

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476604

You could use an IEnumerable<String> method:

public IEnumerable<String> GenerateStrings (IEnumerable<char> characters, int length) {
    if(length > 0) {
        foreach(char c in characters) {
            foreach(String suffix in GenerateStrings(characters,length-1)) {
                yield return c+suffix;
            }
        }
    } else {
        yield return string.Empty;
    }
}

Result with csharp (interactive C# shell):

csharp> Foo.GenerateStrings(new char[] {'A','a','6','w','#'},3)
{ "AAA", "AAa", "AA6", "AAw", "AA#", "AaA", "Aaa", "Aa6", "Aaw", "Aa#", "A6A", "A6a", "A66", "A6w", "A6#", "AwA", "Awa", "Aw6", "Aww", "Aw#", "A#A", "A#a", "A#6", "A#w", "A##", "aAA", "aAa", "aA6", "aAw", "aA#", "aaA", "aaa", "aa6", "aaw", "aa#", "a6A", "a6a", "a66", "a6w", "a6#", "awA", "awa", "aw6", "aww", "aw#", "a#A", "a#a", "a#6", "a#w", "a##", "6AA", "6Aa", "6A6", "6Aw", "6A#", "6aA", "6aa", "6a6", "6aw", "6a#", "66A", "66a", "666", "66w", "66#", "6wA", "6wa", "6w6", "6ww", "6w#", "6#A", "6#a", "6#6", "6#w", "6##", "wAA", "wAa", "wA6", "wAw", "wA#", "waA", "waa", "wa6", "waw", "wa#", "w6A", "w6a", "w66", "w6w", "w6#", "wwA", "wwa", "ww6", "www", "ww#", "w#A", "w#a", "w#6", "w#w", "w##", "#AA", "#Aa", "#A6", "#Aw", "#A#", "#aA", "#aa", "#a6", "#aw", "#a#", "#6A", "#6a", "#66", "#6w", "#6#", "#wA", "#wa", "#w6", "#ww", "#w#", "##A", "##a", "##6", "##w", "###" }

The advantage of using a method with a yield statement is that it is lazy: if you only need five such strings, not all possible strings will be generated first...

Upvotes: 5

Related Questions