Skip to content

Instantly share code, notes, and snippets.

@montyr75
Created November 24, 2013 21:43
Show Gist options
  • Save montyr75/7632761 to your computer and use it in GitHub Desktop.
Save montyr75/7632761 to your computer and use it in GitHub Desktop.
A Dart function that returns a List of all possible permutations of a given string.
List<String> findAllPermutations(String source) {
List allPermutations = [];
void permutate(List list, int cursor) {
// when the cursor gets this far, we've found one permutation, so save it
if (cursor == list.length) {
allPermutations.add(list);
return;
}
for (int i = cursor; i < list.length; i++) {
List permutation = new List.from(list);
permutation[cursor] = list[i];
permutation[i] = list[cursor];
permutate(permutation, cursor + 1);
}
}
permutate(source.split(''), 0);
List<String> strPermutations = [];
for (List permutation in allPermutations) {
strPermutations.add(permutation.join());
}
return strPermutations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment