javascript-algorithms/src/algorithms/string/permutations/permutateWithoutRepetitions.js
2018-04-17 20:28:35 +03:00

40 lines
1.2 KiB
JavaScript

/**
* @param {string} str
* @return {string[]}
*/
export default function permutateWithoutRepetitions(str) {
if (str.length === 0) {
return [];
}
if (str.length === 1) {
return [str];
}
const permutations = [];
// Get all permutations of string of length (n - 1).
const previousString = str.substring(0, str.length - 1);
const previousPermutations = permutateWithoutRepetitions(previousString);
// Insert last character into every possible position of every previous permutation.
const lastCharacter = str.substring(str.length - 1);
for (
let permutationIndex = 0;
permutationIndex < previousPermutations.length;
permutationIndex += 1
) {
const currentPermutation = previousPermutations[permutationIndex];
// Insert strLastCharacter into every possible position of currentPermutation.
for (let positionIndex = 0; positionIndex <= currentPermutation.length; positionIndex += 1) {
const permutationPrefix = currentPermutation.substr(0, positionIndex);
const permutationSuffix = currentPermutation.substr(positionIndex);
permutations.push(permutationPrefix + lastCharacter + permutationSuffix);
}
}
return permutations;
}