Recursively build permutations by inserting each character at the front of permutations of the remaining characters.
def permute(text):
if len(text) <= 1:
return [text]
output = []
for c in text:
output += map(lambda x: c + x, permute(text.replace(c, "")))
return output
print(permute("abc"))
function permute(text) {
if (text.length <= 1) {
return [text];
}
const results = [];
for (const c of text) {
permute(text.replace(c, '')).forEach(item => {
results.push(c + item);
});
}
return results;
}
console.log(permute("abc"))