-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0267-palindrome-permutation-ii.js
More file actions
62 lines (54 loc) · 1.43 KB
/
0267-palindrome-permutation-ii.js
File metadata and controls
62 lines (54 loc) · 1.43 KB
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
56
57
58
59
60
61
62
/**
* 267. Palindrome Permutation II
* https://leetcode.com/problems/palindrome-permutation-ii/
* Difficulty: Medium
*
* Given a string s, return all the palindromic permutations (without duplicates) of it.
*
* You may return the answer in any order. If s has no palindromic permutation, return an
* empty list.
*/
/**
* @param {string} s
* @return {string[]}
*/
var generatePalindromes = function(s) {
const map = new Map();
for (const char of s) {
map.set(char, (map.get(char) || 0) + 1);
}
let oddChar = '';
const half = [];
let oddCount = 0;
for (const [char, count] of map) {
if (count % 2) {
oddCount++;
oddChar = char;
}
for (let i = 0; i < Math.floor(count / 2); i++) {
half.push(char);
}
}
if (oddCount > 1) return [];
const result = new Set();
half.sort();
permute(half, [], new Array(half.length).fill(false));
return Array.from(result);
function permute(chars, current, used) {
if (current.length === chars.length) {
const palindrome = current.join('') + oddChar + current.reverse().join('');
result.add(palindrome);
current.reverse();
return;
}
for (let i = 0; i < chars.length; i++) {
if (!used[i] && (i === 0 || chars[i] !== chars[i - 1] || used[i - 1])) {
used[i] = true;
current.push(chars[i]);
permute(chars, current, used);
current.pop();
used[i] = false;
}
}
}
};