forked from HeyDaniyar/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation-codewar.js
55 lines (49 loc) · 1.39 KB
/
permutation-codewar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*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.
Examples:
permutations('a'); // ['a']
permutations('ab'); // ['ab', 'ba']
permutations('aabb'); // ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
The order of the permutations doesn't matter.
*/
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;
}
//others
function permutations(string) {
var head = string[0];
var tail = string.split('');
tail.splice(0,1);
return g(r(head,tail.join('')));
}
function r(x,str){
if(!str){return [x]}
var head = str[0];
var tail = str.split('');
tail.splice(0,1);
var before = r(head,tail.join(''));
var rst = [];
for(var i = 0; i < before.length;i++){
for(var j = 0; j <= before[i].length; j++){
var tmp = before[i].split('');
tmp.splice(j,0,x)
rst.push(tmp.join(''))
}
}
return rst;
}
function g(a){
var b = [];
a.forEach(function(v,index){
if(a.indexOf(v) === index) b.push(v);
})
return b;