Skip to content

Instantly share code, notes, and snippets.

@yammik
Created January 28, 2019 22:30
Show Gist options
  • Save yammik/ebeb878908391cc957fcc63594e9638d to your computer and use it in GitHub Desktop.
Save yammik/ebeb878908391cc957fcc63594e9638d to your computer and use it in GitHub Desktop.
Creating permutations of a string
function perm(str) {
const results = [];
if (str.length === 1) return str;
for (let i = 0; i < str.length; i++) {
const currentChar = str[i];
const remainingChars = str.slice(0, i) + str.slice(i+1);
const innerPermutations = perm(remainingChars);
for (let j = 0; j < innerPermutations.length; j++) {
results.push(currentChar + innerPermutations[j]);
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment