-2

I have a string list and i want all permutations of all elements with each others

Example :

var myList = new List<string>{ "AB", "CD", "EF", "GK" };

and as result i want a string like this.

var resultStr = "ABCD,ABEF,ABGK,CDAB,CDEF,CDGK,EFAB,EFCD,EFGK,GKAB,GKCD,GKEF";

note that resultStr doesnt include "ABAB","CDCD","EFEF","GKGK"

is there any short way to do this except double for/foreach loops?

jack
  • 65
  • 1
  • 6
  • 2
    All that has been posted is a program description, but that doesn't tell us what _problem_ you're having. What have you tried, and what troubles did you encounter? Please [edit] your post to include a [valid question](/help/how-to-ask) that we can answer. Reminder: make sure you know what is [on-topic](/help/on-topic); asking us to write the program for you, opinions, and external links are off-topic. – gunr2171 Jun 01 '22 at 19:17
  • Do note that the number of results for `n` input strings is `n factorial`. Which means 13 input strings would result in over 6 billion results, which is already more than the maximum array size of ~2.1 billions that .NET can handle. You probably will want to limit the input length accordingly. – Crusha K. Rool Jun 01 '22 at 19:22
  • 1
    Does [this answer](https://stackoverflow.com/questions/33336540/how-to-use-linq-to-find-all-combinations-of-n-items-from-a-set-of-numbers) help? – Peter Smith Jun 01 '22 at 19:24
  • 1
    @CrushaK.Rool It's only O(n^2), or more precisely N*(N-1). I think the factorial case would be if you could pair up to N inputs not just 2 of them. – Dan Is Fiddling By Firelight Jun 01 '22 at 19:31
  • @CrushaK.Rool actually i was plannig to do this with a string list which has almost 50 elements :( I was asking that can i do this without computing n factorial? – jack Jun 01 '22 at 19:32
  • How about `myList.SelectMany(a => myList.Where(b => b != a).Select(b => a + b)).ToList()`? or the LINQ syntax version, `(from a in myList from b in myList where a != b select a+b).ToList()`? – Lasse V. Karlsen Jun 01 '22 at 19:34
  • @PeterSmith acutally no sir. This is giving me `IEnumarable>` and i was looking for a `resultStr`. Probably converting to a string means extra cost i guess – jack Jun 01 '22 at 19:36
  • @CrushaK.Rool OP is asking for Cartesian Product. Why do you think what they wrote relates what they are looking for (and hence your somewhat overestimated n! complexity of desired answer)? – Alexei Levenkov Jun 01 '22 at 19:59
  • Right, I was basing it off the description and mention of "permutating all elements with each other", without checking that the example is actually describing a different operation. – Crusha K. Rool Jun 01 '22 at 21:44

1 Answers1

1

Q: is there any short way to do this except double for/foreach loops?

Yes, you can use LINQ.

Here's the LINQ syntax expression for your result:

var myList = new List<string>{ "AB", "CD", "EF", "GK" };
var result = from a in myList from b in myList where a != b select a+b;
var resultStr = string.Join(",", result);
Console.WriteLine(resultStr);

You can also use the LINQ extension methods:

var myList = new List<string>{ "AB", "CD", "EF", "GK" };
var result = myList.SelectMany(a => myList.Where(b => b != a).Select(b => a + b));
var resultStr = string.Join(",", result);
Console.WriteLine(resultStr);

Both will output

ABCD,ABEF,ABGK,CDAB,CDEF,CDGK,EFAB,EFCD,EFGK,GKAB,GKCD,GKEF

.NET Fiddle so you can try it

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • It may be good idea to mention that OP is looking for variation of [Cartesian Product](https://stackoverflow.com/questions/28401961/cartesian-product) and not "permutations" despite what is written in the question/title. – Alexei Levenkov Jun 01 '22 at 20:00
  • By using linq lambdas as in example#2 you have an option to use indexes for comparison: var result = myList.SelectMany((a,i) => myList.Where((b,j) => i != j).Select(b => a + b)); – Yevhen Cherkes Jun 01 '22 at 20:29