Skip to content

Commit e529fe4

Browse files
committed
feat: add solutions to lc problem: No.0409
No.0409.Longest Palindrome
1 parent a85856a commit e529fe4

File tree

6 files changed

+157
-134
lines changed

6 files changed

+157
-134
lines changed

solution/0400-0499/0409.Longest Palindrome/README.md

+64-57
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50-
> 一个回文字符串,最多存在一个出现奇数次数的字符,
50+
**方法一:计数**
5151

52-
先统计所有字符出现的次数,通用的方式是哈希表。题目已说明只存在大小写字母(52 种可能),也可以使用数组来存储
52+
一个合法的回文字符串,最多存在一个出现奇数次数的字符,其余字符出现次数均为偶数
5353

54-
而后,可分两种方式:
54+
因此,我们可以先遍历字符串 $s$,统计每个字符出现的次数,记录在数组或哈希表 $cnt$ 中。
5555

56-
- 布尔变量
57-
- 累加出现次数为偶数的数值。
58-
- 对于奇数,第一次出现,完整累加;后续出现,则需要对次数 `-1` 去奇,再累加。
59-
- 计数器
60-
- 记录奇数出现的次数,最后的结果回文长度由 `s.length - count` 得知。
61-
- 如果只存在一个奇数,那么可以直接返回 `s.length`.
56+
然后,我们遍历 $cnt$,对于每个字符 $c$,如果 $cnt[c]$ 为偶数,则直接将 $cnt[c]$ 累加到答案 $ans$ 中;如果 $cnt[c]$ 为奇数,则将 $cnt[c] - 1$ 累加到 $ans$ 中,如果 $ans$ 为偶数,则将 $ans$ 增加 $1$。
57+
58+
最后,我们返回 $ans$ 即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度;而 $C$ 为字符集的大小,本题中 $C = 128$。
6261

6362
<!-- tabs:start -->
6463

@@ -69,10 +68,12 @@
6968
```python
7069
class Solution:
7170
def longestPalindrome(self, s: str) -> int:
72-
n = len(s)
73-
counter = Counter(s)
74-
odd_cnt = sum(e % 2 for e in counter.values())
75-
return n if odd_cnt == 0 else n - odd_cnt + 1
71+
cnt = Counter(s)
72+
ans = 0
73+
for v in cnt.values():
74+
ans += v - (v & 1)
75+
ans += (ans & 1 ^ 1) and (v & 1)
76+
return ans
7677
```
7778

7879
### **Java**
@@ -82,20 +83,62 @@ class Solution:
8283
```java
8384
class Solution {
8485
public int longestPalindrome(String s) {
85-
int[] counter = new int[128];
86-
for (char c : s.toCharArray()) {
87-
++counter[c];
86+
int[] cnt = new int[128];
87+
for (int i = 0; i < s.length(); ++i) {
88+
++cnt[s.charAt(i)];
8889
}
89-
int oddCnt = 0;
90-
for (int e : counter) {
91-
oddCnt += (e % 2);
90+
int ans = 0;
91+
for (int v : cnt) {
92+
ans += v - (v & 1);
93+
if (ans % 2 == 0 && v % 2 == 1) {
94+
++ans;
95+
}
9296
}
93-
int n = s.length();
94-
return oddCnt == 0 ? n : n - oddCnt + 1;
97+
return ans;
9598
}
9699
}
97100
```
98101

102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int longestPalindrome(string s) {
108+
int cnt[128]{};
109+
for (char& c : s) {
110+
++cnt[c];
111+
}
112+
int ans = 0;
113+
for (int v : cnt) {
114+
ans += v - (v & 1);
115+
if (ans % 2 == 0 && v % 2 == 1) {
116+
++ans;
117+
}
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
### **Go**
125+
126+
```go
127+
func longestPalindrome(s string) (ans int) {
128+
cnt := [128]int{}
129+
for _, c := range s {
130+
cnt[c]++
131+
}
132+
for _, v := range cnt {
133+
ans += v - (v & 1)
134+
if ans&1 == 0 && v&1 == 1 {
135+
ans++
136+
}
137+
}
138+
return
139+
}
140+
```
141+
99142
### **TypeScript**
100143

101144
```ts
@@ -133,42 +176,6 @@ function longestPalindrome(s: string): number {
133176
}
134177
```
135178

136-
### **C++**
137-
138-
```cpp
139-
class Solution {
140-
public:
141-
int longestPalindrome(string s) {
142-
vector<int> counter(128);
143-
for (char c : s) ++counter[c];
144-
int oddCnt = 0;
145-
for (int e : counter) oddCnt += e % 2;
146-
int n = s.size();
147-
return oddCnt == 0 ? n : n - oddCnt + 1;
148-
}
149-
};
150-
```
151-
152-
### **Go**
153-
154-
```go
155-
func longestPalindrome(s string) int {
156-
counter := make([]int, 128)
157-
for _, c := range s {
158-
counter[c]++
159-
}
160-
oddCnt := 0
161-
for _, e := range counter {
162-
oddCnt += e % 2
163-
}
164-
n := len(s)
165-
if oddCnt == 0 {
166-
return n
167-
}
168-
return n - oddCnt + 1
169-
}
170-
```
171-
172179
### **Rust**
173180

174181
```rust

solution/0400-0499/0409.Longest Palindrome/README_EN.md

+56-48
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,75 @@
4242
```python
4343
class Solution:
4444
def longestPalindrome(self, s: str) -> int:
45-
n = len(s)
46-
counter = Counter(s)
47-
odd_cnt = sum(e % 2 for e in counter.values())
48-
return n if odd_cnt == 0 else n - odd_cnt + 1
45+
cnt = Counter(s)
46+
ans = 0
47+
for v in cnt.values():
48+
ans += v - (v & 1)
49+
ans += (ans & 1 ^ 1) and (v & 1)
50+
return ans
4951
```
5052

5153
### **Java**
5254

5355
```java
5456
class Solution {
5557
public int longestPalindrome(String s) {
56-
int[] counter = new int[128];
57-
for (char c : s.toCharArray()) {
58-
++counter[c];
58+
int[] cnt = new int[128];
59+
for (int i = 0; i < s.length(); ++i) {
60+
++cnt[s.charAt(i)];
5961
}
60-
int oddCnt = 0;
61-
for (int e : counter) {
62-
oddCnt += (e % 2);
62+
int ans = 0;
63+
for (int v : cnt) {
64+
ans += v - (v & 1);
65+
if (ans % 2 == 0 && v % 2 == 1) {
66+
++ans;
67+
}
6368
}
64-
int n = s.length();
65-
return oddCnt == 0 ? n : n - oddCnt + 1;
69+
return ans;
6670
}
6771
}
6872
```
6973

74+
### **C++**
75+
76+
```cpp
77+
class Solution {
78+
public:
79+
int longestPalindrome(string s) {
80+
int cnt[128]{};
81+
for (char& c : s) {
82+
++cnt[c];
83+
}
84+
int ans = 0;
85+
for (int v : cnt) {
86+
ans += v - (v & 1);
87+
if (ans % 2 == 0 && v % 2 == 1) {
88+
++ans;
89+
}
90+
}
91+
return ans;
92+
}
93+
};
94+
```
95+
96+
### **Go**
97+
98+
```go
99+
func longestPalindrome(s string) (ans int) {
100+
cnt := [128]int{}
101+
for _, c := range s {
102+
cnt[c]++
103+
}
104+
for _, v := range cnt {
105+
ans += v - (v & 1)
106+
if ans&1 == 0 && v&1 == 1 {
107+
ans++
108+
}
109+
}
110+
return
111+
}
112+
```
113+
70114
### **TypeScript**
71115

72116
```ts
@@ -104,42 +148,6 @@ function longestPalindrome(s: string): number {
104148
}
105149
```
106150

107-
### **C++**
108-
109-
```cpp
110-
class Solution {
111-
public:
112-
int longestPalindrome(string s) {
113-
vector<int> counter(128);
114-
for (char c : s) ++counter[c];
115-
int oddCnt = 0;
116-
for (int e : counter) oddCnt += e % 2;
117-
int n = s.size();
118-
return oddCnt == 0 ? n : n - oddCnt + 1;
119-
}
120-
};
121-
```
122-
123-
### **Go**
124-
125-
```go
126-
func longestPalindrome(s string) int {
127-
counter := make([]int, 128)
128-
for _, c := range s {
129-
counter[c]++
130-
}
131-
oddCnt := 0
132-
for _, e := range counter {
133-
oddCnt += e % 2
134-
}
135-
n := len(s)
136-
if oddCnt == 0 {
137-
return n
138-
}
139-
return n - oddCnt + 1
140-
}
141-
```
142-
143151
### **Rust**
144152

145153
```rust
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
class Solution {
22
public:
33
int longestPalindrome(string s) {
4-
vector<int> counter(128);
5-
for (char c : s) ++counter[c];
6-
int oddCnt = 0;
7-
for (int e : counter) oddCnt += e % 2;
8-
int n = s.size();
9-
return oddCnt == 0 ? n : n - oddCnt + 1;
4+
int cnt[128]{};
5+
for (char& c : s) {
6+
++cnt[c];
7+
}
8+
int ans = 0;
9+
for (int v : cnt) {
10+
ans += v - (v & 1);
11+
if (ans % 2 == 0 && v % 2 == 1) {
12+
++ans;
13+
}
14+
}
15+
return ans;
1016
}
1117
};
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
func longestPalindrome(s string) int {
2-
counter := make([]int, 128)
1+
func longestPalindrome(s string) (ans int) {
2+
cnt := [128]int{}
33
for _, c := range s {
4-
counter[c]++
4+
cnt[c]++
55
}
6-
oddCnt := 0
7-
for _, e := range counter {
8-
oddCnt += e % 2
6+
for _, v := range cnt {
7+
ans += v - (v & 1)
8+
if ans&1 == 0 && v&1 == 1 {
9+
ans++
10+
}
911
}
10-
n := len(s)
11-
if oddCnt == 0 {
12-
return n
13-
}
14-
return n - oddCnt + 1
12+
return
1513
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
class Solution {
22
public int longestPalindrome(String s) {
3-
int[] counter = new int[128];
4-
for (char c : s.toCharArray()) {
5-
++counter[c];
3+
int[] cnt = new int[128];
4+
for (int i = 0; i < s.length(); ++i) {
5+
++cnt[s.charAt(i)];
66
}
7-
int oddCnt = 0;
8-
for (int e : counter) {
9-
oddCnt += (e % 2);
7+
int ans = 0;
8+
for (int v : cnt) {
9+
ans += v - (v & 1);
10+
if (ans % 2 == 0 && v % 2 == 1) {
11+
++ans;
12+
}
1013
}
11-
int n = s.length();
12-
return oddCnt == 0 ? n : n - oddCnt + 1;
14+
return ans;
1315
}
1416
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Solution:
22
def longestPalindrome(self, s: str) -> int:
3-
n = len(s)
4-
counter = Counter(s)
5-
odd_cnt = sum(e % 2 for e in counter.values())
6-
return n if odd_cnt == 0 else n - odd_cnt + 1
3+
cnt = Counter(s)
4+
ans = 0
5+
for v in cnt.values():
6+
ans += v - (v & 1)
7+
ans += (ans & 1 ^ 1) and (v & 1)
8+
return ans

0 commit comments

Comments
 (0)