Skip to content

Commit 24d51df

Browse files
committed
feat: add solutions to lc problem: No.0267
No.0267.Palindrome Permutation II
1 parent 5cacd70 commit 24d51df

File tree

6 files changed

+416
-2
lines changed

6 files changed

+416
-2
lines changed

solution/0200-0299/0267.Palindrome Permutation II/README.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,168 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
**方法一:回溯**
44+
45+
回文排列需要满足至多有一个字符出现奇数次数。若不满足条件,答案提前返回。
46+
47+
找到出现奇数次的字符,作为中间字符(可以为空),分别向两边扩展,构造回文串。若串的长度与原串长度相等,将该串添加到答案中。
48+
49+
时间复杂度 $O(n \times \frac{n}{2}!)$。其中 $n$ 为字符串 $s$ 的长度。
50+
4351
<!-- tabs:start -->
4452

4553
### **Python3**
4654

4755
<!-- 这里可写当前语言的特殊实现逻辑 -->
4856

4957
```python
50-
58+
class Solution:
59+
def generatePalindromes(self, s: str) -> List[str]:
60+
def dfs(t):
61+
if len(t) == len(s):
62+
ans.append(t)
63+
return
64+
for c, v in cnt.items():
65+
if v > 1:
66+
cnt[c] -= 2
67+
dfs(c + t + c)
68+
cnt[c] += 2
69+
70+
cnt = Counter(s)
71+
mid = ''
72+
for c, v in cnt.items():
73+
if v & 1:
74+
if mid:
75+
return []
76+
mid = c
77+
cnt[c] -= 1
78+
ans = []
79+
dfs(mid)
80+
return ans
5181
```
5282

5383
### **Java**
5484

5585
<!-- 这里可写当前语言的特殊实现逻辑 -->
5686

5787
```java
88+
class Solution {
89+
private List<String> ans = new ArrayList<>();
90+
private int[] cnt = new int[26];
91+
private int n;
92+
93+
public List<String> generatePalindromes(String s) {
94+
n = s.length();
95+
for (char c : s.toCharArray()) {
96+
++cnt[c - 'a'];
97+
}
98+
String mid = "";
99+
for (int i = 0; i < 26; ++i) {
100+
if (cnt[i] % 2 == 1) {
101+
if (!"".equals(mid)) {
102+
return ans;
103+
}
104+
mid = String.valueOf((char) (i + 'a'));
105+
}
106+
}
107+
dfs(mid);
108+
return ans;
109+
}
110+
111+
private void dfs(String t) {
112+
if (t.length() == n) {
113+
ans.add(t);
114+
return;
115+
}
116+
for (int i = 0; i < 26; ++i) {
117+
if (cnt[i] > 1) {
118+
String c = String.valueOf((char) (i + 'a'));
119+
cnt[i] -= 2;
120+
dfs(c + t + c);
121+
cnt[i] += 2;
122+
}
123+
}
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int n;
134+
vector<string> ans;
135+
unordered_map<char, int> cnt;
136+
137+
vector<string> generatePalindromes(string s) {
138+
n = s.size();
139+
for (char c : s) ++cnt[c];
140+
string mid = "";
141+
for (auto& [k, v] : cnt) {
142+
if (v & 1) {
143+
if (mid != "") {
144+
return ans;
145+
}
146+
mid += k;
147+
}
148+
}
149+
dfs(mid);
150+
return ans;
151+
}
152+
153+
void dfs(string t) {
154+
if (t.size() == n) {
155+
ans.push_back(t);
156+
return;
157+
}
158+
for (auto& [k, v] : cnt) {
159+
if (v > 1) {
160+
v -= 2;
161+
dfs(k + t + k);
162+
v += 2;
163+
}
164+
}
165+
}
166+
};
167+
```
58168
169+
### **Go**
170+
171+
```go
172+
func generatePalindromes(s string) []string {
173+
cnt := map[byte]int{}
174+
for i := range s {
175+
cnt[s[i]]++
176+
}
177+
mid := ""
178+
ans := []string{}
179+
for k, v := range cnt {
180+
if v%2 == 1 {
181+
if mid != "" {
182+
return ans
183+
}
184+
mid = string(k)
185+
}
186+
}
187+
var dfs func(t string)
188+
dfs = func(t string) {
189+
if len(t) == len(s) {
190+
ans = append(ans, t)
191+
return
192+
}
193+
for k, v := range cnt {
194+
if v > 1 {
195+
cnt[k] -= 2
196+
c := string(k)
197+
dfs(c + t + c)
198+
cnt[k] += 2
199+
}
200+
}
201+
}
202+
dfs(mid)
203+
return ans
204+
}
59205
```
60206

61207
### **...**

solution/0200-0299/0267.Palindrome Permutation II/README_EN.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,151 @@
3131
### **Python3**
3232

3333
```python
34-
34+
class Solution:
35+
def generatePalindromes(self, s: str) -> List[str]:
36+
def dfs(t):
37+
if len(t) == len(s):
38+
ans.append(t)
39+
return
40+
for c, v in cnt.items():
41+
if v > 1:
42+
cnt[c] -= 2
43+
dfs(c + t + c)
44+
cnt[c] += 2
45+
46+
cnt = Counter(s)
47+
mid = ''
48+
for c, v in cnt.items():
49+
if v & 1:
50+
if mid:
51+
return []
52+
mid = c
53+
cnt[c] -= 1
54+
ans = []
55+
dfs(mid)
56+
return ans
3557
```
3658

3759
### **Java**
3860

3961
```java
62+
class Solution {
63+
private List<String> ans = new ArrayList<>();
64+
private int[] cnt = new int[26];
65+
private int n;
66+
67+
public List<String> generatePalindromes(String s) {
68+
n = s.length();
69+
for (char c : s.toCharArray()) {
70+
++cnt[c - 'a'];
71+
}
72+
String mid = "";
73+
for (int i = 0; i < 26; ++i) {
74+
if (cnt[i] % 2 == 1) {
75+
if (!"".equals(mid)) {
76+
return ans;
77+
}
78+
mid = String.valueOf((char) (i + 'a'));
79+
}
80+
}
81+
dfs(mid);
82+
return ans;
83+
}
84+
85+
private void dfs(String t) {
86+
if (t.length() == n) {
87+
ans.add(t);
88+
return;
89+
}
90+
for (int i = 0; i < 26; ++i) {
91+
if (cnt[i] > 1) {
92+
String c = String.valueOf((char) (i + 'a'));
93+
cnt[i] -= 2;
94+
dfs(c + t + c);
95+
cnt[i] += 2;
96+
}
97+
}
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int n;
108+
vector<string> ans;
109+
unordered_map<char, int> cnt;
110+
111+
vector<string> generatePalindromes(string s) {
112+
n = s.size();
113+
for (char c : s) ++cnt[c];
114+
string mid = "";
115+
for (auto& [k, v] : cnt) {
116+
if (v & 1) {
117+
if (mid != "") {
118+
return ans;
119+
}
120+
mid += k;
121+
}
122+
}
123+
dfs(mid);
124+
return ans;
125+
}
126+
127+
void dfs(string t) {
128+
if (t.size() == n) {
129+
ans.push_back(t);
130+
return;
131+
}
132+
for (auto& [k, v] : cnt) {
133+
if (v > 1) {
134+
v -= 2;
135+
dfs(k + t + k);
136+
v += 2;
137+
}
138+
}
139+
}
140+
};
141+
```
40142
143+
### **Go**
144+
145+
```go
146+
func generatePalindromes(s string) []string {
147+
cnt := map[byte]int{}
148+
for i := range s {
149+
cnt[s[i]]++
150+
}
151+
mid := ""
152+
ans := []string{}
153+
for k, v := range cnt {
154+
if v%2 == 1 {
155+
if mid != "" {
156+
return ans
157+
}
158+
mid = string(k)
159+
}
160+
}
161+
var dfs func(t string)
162+
dfs = func(t string) {
163+
if len(t) == len(s) {
164+
ans = append(ans, t)
165+
return
166+
}
167+
for k, v := range cnt {
168+
if v > 1 {
169+
cnt[k] -= 2
170+
c := string(k)
171+
dfs(c + t + c)
172+
cnt[k] += 2
173+
}
174+
}
175+
}
176+
dfs(mid)
177+
return ans
178+
}
41179
```
42180

43181
### **...**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
int n;
4+
vector<string> ans;
5+
unordered_map<char, int> cnt;
6+
7+
vector<string> generatePalindromes(string s) {
8+
n = s.size();
9+
for (char c : s) ++cnt[c];
10+
string mid = "";
11+
for (auto& [k, v] : cnt) {
12+
if (v & 1) {
13+
if (mid != "") {
14+
return ans;
15+
}
16+
mid += k;
17+
}
18+
}
19+
dfs(mid);
20+
return ans;
21+
}
22+
23+
void dfs(string t) {
24+
if (t.size() == n) {
25+
ans.push_back(t);
26+
return;
27+
}
28+
for (auto& [k, v] : cnt) {
29+
if (v > 1) {
30+
v -= 2;
31+
dfs(k + t + k);
32+
v += 2;
33+
}
34+
}
35+
}
36+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func generatePalindromes(s string) []string {
2+
cnt := map[byte]int{}
3+
for i := range s {
4+
cnt[s[i]]++
5+
}
6+
mid := ""
7+
ans := []string{}
8+
for k, v := range cnt {
9+
if v%2 == 1 {
10+
if mid != "" {
11+
return ans
12+
}
13+
mid = string(k)
14+
}
15+
}
16+
var dfs func(t string)
17+
dfs = func(t string) {
18+
if len(t) == len(s) {
19+
ans = append(ans, t)
20+
return
21+
}
22+
for k, v := range cnt {
23+
if v > 1 {
24+
cnt[k] -= 2
25+
c := string(k)
26+
dfs(c + t + c)
27+
cnt[k] += 2
28+
}
29+
}
30+
}
31+
dfs(mid)
32+
return ans
33+
}

0 commit comments

Comments
 (0)