forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (22 loc) · 640 Bytes
/
index.js
File metadata and controls
29 lines (22 loc) · 640 Bytes
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
// GET PERMUTATION OF A GIVEN STRING
const getPermutations = (str) => {
let result = [];
if (str.length == 0) {
return result;
}
if (str.length == 1) {
result.push(str);
return result;
}
let currentCharacter = str.charAt(0);
let restOfString = str.substring(1);
let returnResult = getPermutations(restOfString);
for (j = 0; j < returnResult.length; j++) {
for (i = 0; i <= returnResult[j].length; i++) {
let value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
result.push(value);
}
}
return result;
};
module.exports = { getPermutations };