Skip to content

Commit a8cd6a1

Browse files
committed
chore: add daily leetcode post 2131. 连接两字母单词得到的最长回文串
1 parent a41a7ba commit a8cd6a1

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: 2131. 连接两字母单词得到的最长回文串.md
3+
date: '2025/5/25-2:33'
4+
tags:
5+
- - Python
6+
- - Answer
7+
abbrlink: 9fa195e5
8+
---
9+
10+
# QUESTION:
11+
12+
[2131. 连接两字母单词得到的最长回文串.md](https://leetcode.cn/problems/longest-palindrome-by-concatenating-two-letter-words/description/?envType=daily-question&envId=2025-05-25)
13+
14+
# My Think:
15+
16+
The idea comes from the brilliant Ling-nc.
17+
18+
We build a hash map to count the occurrences of each word.
19+
20+
For each word, we check whether its reverse exists in the hash map.
21+
If it does, they can form a palindrome pair—we update the result and decrease the corresponding count.
22+
If the reverse does not exist, we increment the count for the current word.
23+
Finally, we check if there’s any palindromic word that can be used as the center of the palindrome.
24+
25+
Example:
26+
27+
1. Input: ["lc", "cl", "gg", "gg"]
28+
29+
2. "lc" has no pair → stored in the map: { "lc": 1 }
30+
31+
3. "cl" finds "lc" exists → use "lc" + "cl" as a pair → res += 4 → map updated to { "lc": 0 }
32+
33+
4. "gg" has no pair → stored in the map: { "lc": 0, "gg": 1 }
34+
35+
5. Second "gg" finds "gg" exists → use "gg" + "gg" as a pair → res += 4 → map becomes { "lc": 0, "gg": 0 }
36+
37+
Then we check whether the hash map contains any palindromic word (i.e., a word with two identical characters) that can be used as the center of the final palindrome string.
38+
If such a word exists, we can add 2 more to the result.
39+
40+
6. Finding a center word (can only pick one symmetric word)
41+
⚠️ In this example, all "gg" words have been paired, so none is left → no center word is added.
42+
43+
44+
思路来自ling-nc大佬. 构建哈希表,统计每个单词的出现次数。
45+
46+
对于每个单词,检查其逆序是否存在于哈希表中,如果存在,则可以形成一个回文串,更新结果并减少对应的计数。
47+
如果逆序不存在,则将该单词的计数增加。最后检查是否有可以作为中心的回文单词。
48+
举例:
49+
1. 输入 ["lc", "cl", "gg", "gg"]
50+
51+
2. "lc" 没有配对 → 存入 map: { "lc": 1 }
52+
53+
3. "cl" 发现 "lc" 存在 → 成对使用 "lc"+"cl" → res += 4 → map 更新为 { "lc": 0 }
54+
55+
4. "gg" 没有配对 → 存入 map: { "lc": 0, "gg": 1 }
56+
57+
5. 第二个 "gg" 发现 "gg" 存在 → 成对使用 "gg"+"gg" → res += 4 → map: { "lc": 0, "gg": 0 }
58+
接着检查哈希表中是否有可以作为中心的回文单词(即两个字母相同的单词),如果有,则可以增加2到结果中。
59+
60+
6. 第二部分:寻找中间部分(只能选一个对称字符串)
61+
⚠️ 在这个例子中,"gg" 都被配完了,没剩下 → 不会加中间部分。
62+
# Code:
63+
64+
```python
65+
from collections import defaultdict
66+
from typing import List
67+
class Solution:
68+
def longestPalindrome(self, words: List[str]) -> int:
69+
count = defaultdict(int)
70+
res = 0
71+
for word in words:
72+
if count[word[::-1]] > 0:
73+
count[word[::-1]] -= 1
74+
res += 4
75+
else:
76+
count[word] += 1
77+
for key, value in count.items():
78+
if key[0] == key[1] and value > 0:
79+
res += 2
80+
break
81+
return res
82+
```
83+
84+
```typescript
85+
function longestPalindrome(words: string[]): number {
86+
const count: Map<string, number> = new Map();
87+
let res = 0;
88+
89+
for (const word of words) {
90+
const reversed = word.split('').reverse().join('');
91+
const reversedCount = count.get(reversed) ?? 0;
92+
93+
if (reversedCount > 0) {
94+
count.set(reversed, reversedCount - 1);
95+
res += 2 * word.length;
96+
} else {
97+
count.set(word, (count.get(word) ?? 0) + 1);
98+
}
99+
}
100+
101+
for (const [word, freq] of count.entries()) {
102+
if (word === word.split('').reverse().join('') && freq > 0) {
103+
res += word.length;
104+
break; // 只能选一个居中的回文串
105+
}
106+
}
107+
108+
return res;
109+
}
110+
```

0 commit comments

Comments
 (0)