Skip to content

feat: add solutions to lc problem: No.2131 #4428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ tags:

### 方法一:贪心 + 哈希表

我们先用哈希表 `cnt` 统计每个单词出现的次数。
我们先用一个哈希表 $\textit{cnt}$ 统计每个单词出现的次数。

遍历 `cnt` 中的每个单词 $k$ 以及其出现次数 $v$:
遍历 $\textit{cnt}$ 中的每个单词 $k$ 以及其出现次数 $v$:

如果 $k$ 中两个字母相同,那么我们可以将 $\left \lfloor \frac{v}{2} \right \rfloor \times 2$ 个 $k$ 连接到回文串的前后,此时如果 $k$ 还剩余一个,那么我们可以先记录到 $x$ 中。
- 如果 $k$ 中两个字母相同,那么我们可以将 $\left \lfloor \frac{v}{2} \right \rfloor \times 2$ 个 $k$ 连接到回文串的前后,此时如果 $k$ 还剩余一个,那么我们可以先记录到 $x$ 中。

如果 $k$ 中两个字母不同,那么我们要找到一个单词 $k'$,使得 $k'$ 中的两个字母与 $k$ 相反,即 $k' = k[1] + k[0]$。如果 $k'$ 存在,那么我们可以将 $\min(v, cnt[k'])$ 个 $k$ 连接到回文串的前后。
- 如果 $k$ 中两个字母不同,那么我们要找到一个单词 $k'$,使得 $k'$ 中的两个字母与 $k$ 相反,即 $k' = k[1] + k[0]$。如果 $k'$ 存在,那么我们可以将 $\min(v, \textit{cnt}[k'])$ 个 $k$ 连接到回文串的前后。

遍历结束后,如果 $x$ 不为空,那么我们还可以将一个单词连接到回文串的中间。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `words` 的长度
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为单词的数量

<!-- tabs:start -->

Expand Down Expand Up @@ -111,7 +111,7 @@ class Solution {
public int longestPalindrome(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (var w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
cnt.merge(w, 1, Integer::sum);
}
int ans = 0, x = 0;
for (var e : cnt.entrySet()) {
Expand Down Expand Up @@ -183,6 +183,26 @@ func longestPalindrome(words []string) int {
}
```

#### TypeScript

```ts
function longestPalindrome(words: string[]): number {
const cnt = new Map<string, number>();
for (const w of words) cnt.set(w, (cnt.get(w) || 0) + 1);
let [ans, x] = [0, 0];
for (const [k, v] of cnt.entries()) {
if (k[0] === k[1]) {
x += v & 1;
ans += Math.floor(v / 2) * 2 * 2;
} else {
ans += Math.min(v, cnt.get(k[1] + k[0]) || 0) * 2;
}
}
ans += x ? 2 : 0;
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,19 @@ Note that &quot;ll&quot; is another longest palindrome that can be created, and

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Hash Table

First, we use a hash table $\textit{cnt}$ to count the occurrences of each word.

Iterate through each word $k$ and its count $v$ in $\textit{cnt}$:

- If the two letters in $k$ are the same, we can concatenate $\left \lfloor \frac{v}{2} \right \rfloor \times 2$ copies of $k$ to the front and back of the palindrome. If there is one $k$ left, we can record it in $x$ for now.

- If the two letters in $k$ are different, we need to find a word $k'$ such that the two letters in $k'$ are the reverse of $k$, i.e., $k' = k[1] + k[0]$. If $k'$ exists, we can concatenate $\min(v, \textit{cnt}[k'])$ copies of $k$ to the front and back of the palindrome.

After the iteration, if $x$ is not empty, we can also place one word in the middle of the palindrome.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of words.

<!-- tabs:start -->

Expand Down Expand Up @@ -101,7 +113,7 @@ class Solution {
public int longestPalindrome(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (var w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
cnt.merge(w, 1, Integer::sum);
}
int ans = 0, x = 0;
for (var e : cnt.entrySet()) {
Expand Down Expand Up @@ -173,6 +185,26 @@ func longestPalindrome(words []string) int {
}
```

#### TypeScript

```ts
function longestPalindrome(words: string[]): number {
const cnt = new Map<string, number>();
for (const w of words) cnt.set(w, (cnt.get(w) || 0) + 1);
let [ans, x] = [0, 0];
for (const [k, v] of cnt.entries()) {
if (k[0] === k[1]) {
x += v & 1;
ans += Math.floor(v / 2) * 2 * 2;
} else {
ans += Math.min(v, cnt.get(k[1] + k[0]) || 0) * 2;
}
}
ans += x ? 2 : 0;
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Solution {
public int longestPalindrome(String[] words) {
Map<String, Integer> cnt = new HashMap<>();
for (var w : words) {
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
cnt.merge(w, 1, Integer::sum);
}
int ans = 0, x = 0;
for (var e : cnt.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function longestPalindrome(words: string[]): number {
const cnt = new Map<string, number>();
for (const w of words) cnt.set(w, (cnt.get(w) || 0) + 1);
let [ans, x] = [0, 0];
for (const [k, v] of cnt.entries()) {
if (k[0] === k[1]) {
x += v & 1;
ans += Math.floor(v / 2) * 2 * 2;
} else {
ans += Math.min(v, cnt.get(k[1] + k[0]) || 0) * 2;
}
}
ans += x ? 2 : 0;
return ans;
}