fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. static void CollectAll(ISet<String> remaining, IList<string> soFar, List<List<string>> all) {
  8. if (soFar.Count != 0) {
  9. all.Add(soFar.ToList());
  10. }
  11. foreach (var item in remaining.ToList()) {
  12. remaining.Remove(item);
  13. soFar.Add(item);
  14. CollectAll(remaining, soFar, all);
  15. soFar.Remove(item);
  16. remaining.Add(item);
  17. }
  18. }
  19. public static void Main()
  20. {
  21. var items = new HashSet<string> {"a", "b", "c"};
  22. var tmp = new List<string>();
  23. List<List<string>> res = new List<List<string>>();
  24. CollectAll(items, tmp, res);
  25. foreach (var lst in res) {
  26. Console.WriteLine(string.Join(" ", lst.ToArray()));
  27. }
  28. }
  29. }
Success #stdin #stdout 0.04s 24344KB
stdin
Standard input is empty
stdout
a
a b
a b c
a c
a c b
b
b a
b a c
b c
b c a
c
c a
c a b
c b
c b a