Created
November 24, 2013 21:43
-
-
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.
This file contains 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
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