JSFiddle - React, Tailwind, and code Playground

by Fareez Ahmed

JavaScript

function stringPermutation(string) {
  var permutations = [];

  function permuteInner(combo, str) {
    if (str === "") {
      permutations.push(combo);
    }
    for (var i = 0; i < str.length; i++) {
      var beforeCurrent = str.slice(0, i);
      var afterCurrent = str.slice(i + 1);
      permuteInner(combo + str[i], beforeCurrent + afterCurrent);
    }
  }
  permuteInner("", string);
  return permutations;
}

console.log(stringPermutation("abc"));