-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
36 lines (31 loc) · 912 Bytes
/
main.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
var StringOps = (function() {
function permutations(str) {
var pers = '',
permute = function permute(prefix, str) {
var n = str.length;
if (n == 0) {
pers += prefix + ' ';
} else {
for (var i = 0; i < n; i++) {
permute(prefix + str[i], str.substring(0, i) + str.substring(i + 1, n));
}
}
}
permute('', str);
return pers;
}
function swap(str, a, b) {
var t = str[a];
str = replaceAt(str, a, str[b]);
str = replaceAt(str, b, t);
return str;
}
function replaceAt(s, index, character) {
return s.substr(0, index) + character + s.substr(index + character.length);
}
return {
permutations: permutations,
swap: swap,
replaceAt: replaceAt
}
})();