Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Last active May 20, 2019 09:56
Show Gist options
  • Save arialdomartini/87c1c35452357d32d52774c79a226c09 to your computer and use it in GitHub Desktop.
Save arialdomartini/87c1c35452357d32d52774c79a226c09 to your computer and use it in GitHub Desktop.
Permutations of a string, with separate pending and permutations
private static IEnumerable<string> PermutationsIterative(string s)
{
var pending = new List<(string permutation, string rest)>
{
("", s)
};
var permutations = new List<string>();
while (pending.Any())
{
pending = pending
.SelectMany(p =>
p.rest.Select(c =>
(
p.permutation + c,
p.rest.RemoveChar(c)
))).ToList();
permutations = permutations.Union(
pending
.Where(p => p.rest == "")
.Select(p => p.permutation))
.ToList();
}
return permutations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment