Skip to content

Commit a2d6213

Browse files
committed
src/bin/longest-palindrome.rs
1 parent 2e1d178 commit a2d6213

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/bin/longest-palindrome.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {}
2+
3+
struct Solution;
4+
5+
impl Solution {
6+
pub fn longest_palindrome(s: String) -> i32 {
7+
let mut hash = std::collections::HashMap::new();
8+
s.as_bytes().into_iter().for_each(|x| {
9+
hash.entry(x).and_modify(|v| *v += 1).or_insert(1);
10+
});
11+
12+
let mut odd = false; // 是否有单数,如果有单数的话,则最后的结果+1
13+
14+
let mut r = 0;
15+
16+
hash.into_iter().for_each(|(_, v)| if v % 2 == 0 { r += v; } else {
17+
odd = true;
18+
r += v - 1;
19+
});
20+
21+
22+
if odd { r + 1 } else { r }
23+
}
24+
}

0 commit comments

Comments
 (0)