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) · 666 Bytes
/
Copy pathindex.js
File metadata and controls
29 lines (22 loc) · 666 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) => {
const result = [];
if (str.length === 0) {
return result;
}
if (str.length === 1) {
result.push(str);
return result;
}
const currentCharacter = str.charAt(0);
const restOfString = str.substring(1);
const returnResult = getPermutations(restOfString);
for (let j = 0; j < returnResult.length; j += 1) {
for (let i = 0; i <= returnResult[j].length; i += 1) {
const value = returnResult[j].substring(0, i) + currentCharacter + returnResult[j].substring(i);
result.push(value);
}
}
return result;
};
module.exports = { getPermutations };