Created
May 21, 2016 19:59
-
-
Save saip106/40163404ad997908a2d38442319e84ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function swap(array, i, j){ | |
var temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
function findPermutationsInternal(array, start, end){ | |
var result = []; | |
if(start === end){ | |
return [array.join('')]; | |
} | |
for(var i=start; i <= end; i++){ | |
swap(array, start, i); | |
var tempResult = findPermutationsInternal(array, start+1, end); | |
result = result.concat(tempResult); | |
swap(array, start, i); | |
} | |
return result; | |
} | |
function findPermutations(input){ | |
return findPermutationsInternal(input.split(''), 0, input.length -1); | |
} | |
findPermutations('abc'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment