Coding Challenge: Generate all permutations of an input string.

For Example, if you have given input string: ‘abc’ the result should be:

[ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba']

This problem can be solved by separating the first letter and find all permutations of the remaining letters. 

So for “abc” we can do:

a bc
a cb
b ac
b ca
c ab
c ba

Now first let’s convert this details into Pseudocode.

permutations(abc) = a + permutations(bc) +
                    b + permutations(ac) +
                    c + permutations(ab)permutations(ab) = a + permutations(b) +
                   b + permutations(a)permutations(a) = a

Now We have enough understanding to convert this Pseudocode into an actual code.





function permutationsTest(inputstring) { 
  let results = []; 
  if (inputstring.length === 1) { 
    results.push(inputstring); 
    return results; 
  } 
  for (let i = 0; i < inputstring.length; i++) { 
    let firstChar = inputstring[i]; 
    let charsLeft = inputstring.substring(0, i) + inputstring.substring(i + 1); 
    let innerPermutations = permutationsTest(charsLeft); 
    for (let j = 0; j < innerPermutations.length; j++) { 
      results.push(firstChar + innerPermutations[j]); 
    } 
  } 
  return results; 
}