Skip to content

Commit 3f3c528

Browse files
committed
feat: add solutions to lc problem: No.0409
No.0409.Longest Palindrome
1 parent ad81ee6 commit 3f3c528

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,25 @@ function longestPalindrome(s: string): number {
102102
}
103103
```
104104

105+
```ts
106+
function longestPalindrome(s: string): number {
107+
const map = new Map();
108+
for (const c of s) {
109+
map.set(c, (map.get(c) ?? 0) + 1);
110+
}
111+
let hasOdd = false;
112+
let res = 0;
113+
for (const v of map.values()) {
114+
res += v;
115+
if (v & 1) {
116+
hasOdd = true;
117+
res--;
118+
}
119+
}
120+
return res + (hasOdd ? 1 : 0);
121+
}
122+
```
123+
105124
### **C++**
106125

107126
```cpp
@@ -138,6 +157,31 @@ func longestPalindrome(s string) int {
138157
}
139158
```
140159

160+
### **Rust**
161+
162+
```rust
163+
use std::collections::HashMap;
164+
165+
impl Solution {
166+
pub fn longest_palindrome(s: String) -> i32 {
167+
let mut map: HashMap<char, i32> = HashMap::new();
168+
for c in s.chars() {
169+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
170+
}
171+
let mut has_odd = false;
172+
let mut res = 0;
173+
for v in map.values() {
174+
res += v;
175+
if v % 2 == 1 {
176+
has_odd = true;
177+
res -= 1;
178+
}
179+
}
180+
res + if has_odd { 1 } else { 0 }
181+
}
182+
}
183+
```
184+
141185
### **...**
142186

143187
```

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ function longestPalindrome(s: string): number {
9292
}
9393
```
9494

95+
```ts
96+
function longestPalindrome(s: string): number {
97+
const map = new Map();
98+
for (const c of s) {
99+
map.set(c, (map.get(c) ?? 0) + 1);
100+
}
101+
let hasOdd = false;
102+
let res = 0;
103+
for (const v of map.values()) {
104+
res += v;
105+
if (v & 1) {
106+
hasOdd = true;
107+
res--;
108+
}
109+
}
110+
return res + (hasOdd ? 1 : 0);
111+
}
112+
```
113+
95114
### **C++**
96115

97116
```cpp
@@ -128,6 +147,31 @@ func longestPalindrome(s string) int {
128147
}
129148
```
130149

150+
### **Rust**
151+
152+
```rust
153+
use std::collections::HashMap;
154+
155+
impl Solution {
156+
pub fn longest_palindrome(s: String) -> i32 {
157+
let mut map: HashMap<char, i32> = HashMap::new();
158+
for c in s.chars() {
159+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
160+
}
161+
let mut has_odd = false;
162+
let mut res = 0;
163+
for v in map.values() {
164+
res += v;
165+
if v % 2 == 1 {
166+
has_odd = true;
167+
res -= 1;
168+
}
169+
}
170+
res + if has_odd { 1 } else { 0 }
171+
}
172+
}
173+
```
174+
131175
### **...**
132176

133177
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn longest_palindrome(s: String) -> i32 {
5+
let mut map: HashMap<char, i32> = HashMap::new();
6+
for c in s.chars() {
7+
map.insert(c, map.get(&c).unwrap_or(&0) + 1);
8+
}
9+
let mut has_odd = false;
10+
let mut res = 0;
11+
for v in map.values() {
12+
res += v;
13+
if v % 2 == 1 {
14+
has_odd = true;
15+
res -= 1;
16+
}
17+
}
18+
res + if has_odd { 1 } else { 0 }
19+
}
20+
}

0 commit comments

Comments
 (0)