Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const swap = (x, y, a) => {
- const arr = a.slice()
- arr[y] = a[x];
- arr[x] = a[y];
- return arr
- }
- function* heapPermutations(arr) {
- const length = arr.length
- const c = Array.apply([], { length }).map(() => 0)
- let i = 0;
- yield arr
- while (i < length) {
- if (c[i] < i) {
- arr = swap(i%2===0 ? 0 : c[i], i, arr)
- yield arr
- c[i] += 1
- i = 0
- } else {
- c[i] = 0
- i += 1
- }
- }
- }
- const stringPermutations = string => [...heapPermutations(string.split(''))].map(a => a.join(''))
- console.log(stringPermutations('zamagni'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement