Print all permutations of a given string.
Begin at the first letter.
Continue this process for each position in the word. Once we've reached the end of the word, we have found one complete arrangement (permutation).
s = "abc" output = [] def swap(ls, i, j): ls[i], ls[j] = ls[j], ls[i] def permute(ls, idx): if idx == len(ls) - 1: output.append("".join(ls)) return for i in range(idx, len(ls)): swap(ls, idx, i) permute(ls, idx + 1) swap(ls, idx, i) permute(list(s), 0) print(output)
const data = "abc"; const output = []; function swap(list, i, j) { [list[i], list[j]] = [list[j], list[i]]; } function permute(list, idx) { if (idx === list.length) { output.push(list.join("")); return; } for (let i = idx; i < list.length; i++) { swap(list, idx, i); permute(list, idx + 1); swap(list, idx, i); } } permute(data.split(""), 0); console.log(output);