Last active
May 20, 2019 09:56
-
-
Save arialdomartini/87c1c35452357d32d52774c79a226c09 to your computer and use it in GitHub Desktop.
Permutations of a string, with separate pending and permutations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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