/** * Find the permutations of the given string. * @param s The input string * * Algorithm: * 1. If the length of the string is 1, return the string as permutation of 'a' is 'a' * 2. Else for the given string find the last character char and the substring by stripping the last char * 2.1 Recurse with the substring * 3. For each permutation from step 2 add the char to each position of the substring and return */ function permute(s) { var len = s.length, char, i = 0, j = 0, p = [], elem, pArr = [], plen = 0; if (len == 1) return s; char = s.charAt(len-1); pArr = permute(s.substring(0, len-1)); plen = pArr.length; for (i = 0; i < plen; i++) { for (j = 0; j < len; j++) { elem = pArr[i].split(""); elem.splice(j, 0, char); p.push(elem.join("")); } } return [...new Set(p)]; }

permute("abc"); // [ 'cba', 'bca', 'bac', 'cab', 'acb', 'abc' ]Download from github. Adapted from python version by GodComplex2.