We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2e1d178 commit a2d6213Copy full SHA for a2d6213
src/bin/longest-palindrome.rs
@@ -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