Created
September 19, 2020 06:36
-
-
Save hassan-maavan/e35639c61373d84fb50b6136ec243d8f to your computer and use it in GitHub Desktop.
In this kata you have to create all permutations of an input string and remove duplicates, if present. This means, you have to shuffle all letters from the input in all possible orders. https://www.codewars.com/kata/5254ca2719453dcc0b00027d
This file contains 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 permutations(string) { | |
var arr = string.split(''), tmp = arr.slice(), heads = [], out = []; | |
if(string.length == 1) return [string]; | |
arr.forEach(function(v, i, arr) { | |
if(heads.indexOf(v) == -1) { | |
heads.push(v); | |
tmp.splice(tmp.indexOf(v), 1); | |
permutations(tmp.join('')).forEach(function(w) {out.push(v + w);}); | |
tmp.push(v); | |
} | |
}); | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment