-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (26 loc) · 755 Bytes
/
index.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
const combinations = (input) => {
const characters = input.split("");
return _generateCombinations(characters);
};
const palindrome = (input) => {
const items = combinations(input);
return [
...new Set(
items.filter((word) => word === word.split("").reverse().join(""))
),
];
};
const _generateCombinations = (characters, current = "") => {
let result = [];
characters.forEach((character, index) => {
const newCurrent = current + character;
result.push(newCurrent);
const otherCharacters = [...characters];
otherCharacters.splice(index, 1);
_generateCombinations(otherCharacters, newCurrent).forEach((word) =>
result.push(word)
);
});
return result;
};
export { combinations, palindrome };