I would like to generate permutations of string words that are inputted from a file. I know the count and was wondering if there is an easy way to do this using an arraylist.
A:
Since this doesn't have a homework
tag, I'd suggest using a std::vector
of words and std::next_permutation
. (If it had a homework
tag, I'd suggest how to implement something like std::next_permutation
.)
sbi
2010-04-10 16:35:58
Sometimes all these libraries take the fun out of programming ;)
WhirlWind
2010-04-10 16:37:49
Its not homework...I have a bunch of nouns I compiled by using SharpNLP and now wish to produce permutations of length 5 using each noun. I realize its a recursive method required but not sure how to implement it since all searches on this does not produce a clear method. But I will keep checking thanks..
vbNewbie
2010-04-10 19:06:26
+1
A:
Great article on MSDN Magazine: String Permutations
Combination Generator in Linq (this one has a LINQ based answer)
Using the code provided in the above link:
string str = "leniel";
var permutations = GetPermutations(str);
foreach (string s in permutations)
{
Console.WriteLine(s);
}
Console.WriteLine(permutations.Count()); // 720 permutations
Console.ReadLine();
More links to help:
Listing all permutations of a string/integer
Generating (word) combinations (permutations) out of a string
Is there a .NET library that can do string permutations or string expansion?
Leniel Macaferi
2010-04-10 16:42:38
Thanks for the help. I just realized I had it wrong and what I need to do is combinations of the strings with the order not changing but selection does. Also it should be n(n-1)/2 results.
vbNewbie
2010-04-12 03:54:10