Skip to content

Commit 937eb8d

Browse files
committed
feat: add solutions to lc problem: No.2131
No.2131.Longest Palindrome by Concatenating Two Letter Words
1 parent 73811d4 commit 937eb8d

File tree

6 files changed

+281
-2
lines changed

6 files changed

+281
-2
lines changed

solution/2100-2199/2131.Longest Palindrome by Concatenating Two Letter Words/README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,127 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心 + 哈希表**
57+
58+
我们先用哈希表 `cnt` 统计每个单词出现的次数。
59+
60+
遍历 `cnt` 中的每个单词 $k$ 以及其出现次数 $v$:
61+
62+
如果 $k$ 中两个字母相同,那么我们可以将 $\left \lfloor \frac{v}{2} \right \rfloor \times 2$ 个 $k$ 连接到回文串的前后,此时如果 $k$ 还剩余一个,那么我们可以先记录到 $x$ 中。
63+
64+
如果 $k$ 中两个字母不同,那么我们要找到一个单词 $k'$,使得 $k'$ 中的两个字母与 $k$ 相反,即 $k' = k[1] + k[0]$。如果 $k'$ 存在,那么我们可以将 $\min(v, cnt[k'])$ 个 $k$ 连接到回文串的前后。
65+
66+
遍历结束后,如果 $x$ 不为空,那么我们还可以将一个单词连接到回文串的中间。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `words` 的长度。
69+
5670
<!-- tabs:start -->
5771

5872
### **Python3**
5973

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

6276
```python
63-
77+
class Solution:
78+
def longestPalindrome(self, words: List[str]) -> int:
79+
cnt = Counter(words)
80+
ans = x = 0
81+
for k, v in cnt.items():
82+
if k[0] == k[1]:
83+
x += v & 1
84+
ans += v // 2 * 2 * 2
85+
else:
86+
ans += min(v, cnt[k[::-1]]) * 2
87+
ans += 2 if x else 0
88+
return ans
6489
```
6590

6691
### **Java**
6792

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

7095
```java
96+
class Solution {
97+
public int longestPalindrome(String[] words) {
98+
Map<String, Integer> cnt = new HashMap<>();
99+
for (var w : words) {
100+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
101+
}
102+
int ans = 0, x = 0;
103+
for (var e : cnt.entrySet()) {
104+
var k = e.getKey();
105+
var rk = new StringBuilder(k).reverse().toString();
106+
int v = e.getValue();
107+
if (k.charAt(0) == k.charAt(1)) {
108+
x += v & 1;
109+
ans += v / 2 * 2 * 2;
110+
} else {
111+
ans += Math.min(v, cnt.getOrDefault(rk, 0)) * 2;
112+
}
113+
}
114+
ans += x > 0 ? 2 : 0;
115+
return ans;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
int longestPalindrome(vector<string>& words) {
126+
unordered_map<string, int> cnt;
127+
for (auto& w : words) cnt[w]++;
128+
int ans = 0, x = 0;
129+
for (auto& [k, v] : cnt) {
130+
string rk = k;
131+
reverse(rk.begin(), rk.end());
132+
if (k[0] == k[1]) {
133+
x += v & 1;
134+
ans += v / 2 * 2 * 2;
135+
} else if (cnt.count(rk)) {
136+
ans += min(v, cnt[rk]) * 2;
137+
}
138+
}
139+
ans += x ? 2 : 0;
140+
return ans;
141+
}
142+
};
143+
```
71144
145+
## **Go**
146+
147+
```go
148+
func longestPalindrome(words []string) int {
149+
cnt := map[string]int{}
150+
for _, w := range words {
151+
cnt[w]++
152+
}
153+
ans, x := 0, 0
154+
for k, v := range cnt {
155+
if k[0] == k[1] {
156+
x += v & 1
157+
ans += v / 2 * 2 * 2
158+
} else {
159+
rk := string([]byte{k[1], k[0]})
160+
if y, ok := cnt[rk]; ok {
161+
ans += min(v, y) * 2
162+
}
163+
}
164+
}
165+
if x > 0 {
166+
ans += 2
167+
}
168+
return ans
169+
}
170+
171+
func min(a, b int) int {
172+
if a < b {
173+
return a
174+
}
175+
return b
176+
}
72177
```
73178

74179
### **TypeScript**

solution/2100-2199/2131.Longest Palindrome by Concatenating Two Letter Words/README_EN.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,104 @@ Note that &quot;ll&quot; is another longest palindrome that can be created, and
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def longestPalindrome(self, words: List[str]) -> int:
61+
cnt = Counter(words)
62+
ans = x = 0
63+
for k, v in cnt.items():
64+
if k[0] == k[1]:
65+
x += v & 1
66+
ans += v // 2 * 2 * 2
67+
else:
68+
ans += min(v, cnt[k[::-1]]) * 2
69+
ans += 2 if x else 0
70+
return ans
6071
```
6172

6273
### **Java**
6374

6475
```java
76+
class Solution {
77+
public int longestPalindrome(String[] words) {
78+
Map<String, Integer> cnt = new HashMap<>();
79+
for (var w : words) {
80+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
81+
}
82+
int ans = 0, x = 0;
83+
for (var e : cnt.entrySet()) {
84+
var k = e.getKey();
85+
var rk = new StringBuilder(k).reverse().toString();
86+
int v = e.getValue();
87+
if (k.charAt(0) == k.charAt(1)) {
88+
x += v & 1;
89+
ans += v / 2 * 2 * 2;
90+
} else {
91+
ans += Math.min(v, cnt.getOrDefault(rk, 0)) * 2;
92+
}
93+
}
94+
ans += x > 0 ? 2 : 0;
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int longestPalindrome(vector<string>& words) {
106+
unordered_map<string, int> cnt;
107+
for (auto& w : words) cnt[w]++;
108+
int ans = 0, x = 0;
109+
for (auto& [k, v] : cnt) {
110+
string rk = k;
111+
reverse(rk.begin(), rk.end());
112+
if (k[0] == k[1]) {
113+
x += v & 1;
114+
ans += v / 2 * 2 * 2;
115+
} else if (cnt.count(rk)) {
116+
ans += min(v, cnt[rk]) * 2;
117+
}
118+
}
119+
ans += x ? 2 : 0;
120+
return ans;
121+
}
122+
};
123+
```
65124
125+
## **Go**
126+
127+
```go
128+
func longestPalindrome(words []string) int {
129+
cnt := map[string]int{}
130+
for _, w := range words {
131+
cnt[w]++
132+
}
133+
ans, x := 0, 0
134+
for k, v := range cnt {
135+
if k[0] == k[1] {
136+
x += v & 1
137+
ans += v / 2 * 2 * 2
138+
} else {
139+
rk := string([]byte{k[1], k[0]})
140+
if y, ok := cnt[rk]; ok {
141+
ans += min(v, y) * 2
142+
}
143+
}
144+
}
145+
if x > 0 {
146+
ans += 2
147+
}
148+
return ans
149+
}
150+
151+
func min(a, b int) int {
152+
if a < b {
153+
return a
154+
}
155+
return b
156+
}
66157
```
67158

68159
### **TypeScript**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int longestPalindrome(vector<string>& words) {
4+
unordered_map<string, int> cnt;
5+
for (auto& w : words) cnt[w]++;
6+
int ans = 0, x = 0;
7+
for (auto& [k, v] : cnt) {
8+
string rk = k;
9+
reverse(rk.begin(), rk.end());
10+
if (k[0] == k[1]) {
11+
x += v & 1;
12+
ans += v / 2 * 2 * 2;
13+
} else if (cnt.count(rk)) {
14+
ans += min(v, cnt[rk]) * 2;
15+
}
16+
}
17+
ans += x ? 2 : 0;
18+
return ans;
19+
}
20+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func longestPalindrome(words []string) int {
2+
cnt := map[string]int{}
3+
for _, w := range words {
4+
cnt[w]++
5+
}
6+
ans, x := 0, 0
7+
for k, v := range cnt {
8+
if k[0] == k[1] {
9+
x += v & 1
10+
ans += v / 2 * 2 * 2
11+
} else {
12+
rk := string([]byte{k[1], k[0]})
13+
if y, ok := cnt[rk]; ok {
14+
ans += min(v, y) * 2
15+
}
16+
}
17+
}
18+
if x > 0 {
19+
ans += 2
20+
}
21+
return ans
22+
}
23+
24+
func min(a, b int) int {
25+
if a < b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int longestPalindrome(String[] words) {
3+
Map<String, Integer> cnt = new HashMap<>();
4+
for (var w : words) {
5+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
6+
}
7+
int ans = 0, x = 0;
8+
for (var e : cnt.entrySet()) {
9+
var k = e.getKey();
10+
var rk = new StringBuilder(k).reverse().toString();
11+
int v = e.getValue();
12+
if (k.charAt(0) == k.charAt(1)) {
13+
x += v & 1;
14+
ans += v / 2 * 2 * 2;
15+
} else {
16+
ans += Math.min(v, cnt.getOrDefault(rk, 0)) * 2;
17+
}
18+
}
19+
ans += x > 0 ? 2 : 0;
20+
return ans;
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def longestPalindrome(self, words: List[str]) -> int:
3+
cnt = Counter(words)
4+
ans = x = 0
5+
for k, v in cnt.items():
6+
if k[0] == k[1]:
7+
x += v & 1
8+
ans += v // 2 * 2 * 2
9+
else:
10+
ans += min(v, cnt[k[::-1]]) * 2
11+
ans += 2 if x else 0
12+
return ans

0 commit comments

Comments
 (0)