Answer the question
In order to leave comments, you need to log in
How to generate all words from letters of given length?
Help need to generate words from a given alphabet
for example:
alphabet: a, b, c, d
word
length: 4
then the number of words: 4 ^ 2
aa
ab
ag
ba
bb
bc
bg
va
wb
cc
....
y
Answer the question
In order to leave comments, you need to log in
This is not the best, but probably the easiest option.
If you want, I can write in detail.
class Program
{
static void Main(string[] args)
{
Console.InputEncoding = Console.OutputEncoding = Encoding.GetEncoding(@"Cyrillic");
var letters = Console.ReadLine().ToCharArray(); // Введи весь алфавит в одну строчку
var wordLength = int.Parse(Console.ReadLine()); // Введи размер слова
var word = new char[wordLength];
var totalWordsCount = 1;
for (var i = 0; i < wordLength; i++)
totalWordsCount *= letters.Length;
for (var i = 0; i < totalWordsCount; i++)
{
var accum = i;
for (var j = word.Length - 1; j >= 0; j--)
{
word[j] = letters[accum % letters.Length];
accum /= letters.Length;
}
Console.WriteLine(new string(word));
}
Console.ReadKey();
}
}
for the future:
- you need all possible permutations of letters, not all words
- permutations in english will be "permutations"
- google
https://en.wikipedia.org/wiki/Heap%27s_algorithm
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question