-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
32 lines (28 loc) · 845 Bytes
/
main.cpp
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
class Solution {
public:
vector<string> generatePalindromes(string s) {
array<int, 26> charFreq = {};
for (char c : s) ++charFreq[c - 'a'];
string mid = "";
for (int i = 0; i < 26; ++i) {
if (charFreq[i] & 1) {
if (mid != "") return {};
mid += 'a' + i;
}
}
string t;
for (int i = 0; i < 26; ++i) {
int v = charFreq[i];
if (v < 2) continue;
t += string(v / 2, 'a' + i);
}
vector<string> result;
unordered_map<string, bool> seen;
do {
if (seen[t]) continue;
seen[t] = true;
result.push_back(t + mid + string{t.rbegin(), t.rend()});
} while (next_permutation(t.begin(), t.end()));
return result;
}
};