Skip to content

Commit 7dba398

Browse files
committed
Sync LeetCode submission - Longest Palindrome (rust)
1 parent c9bb5be commit 7dba398

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn longest_palindrome(s: String) -> i32 {
4+
let len = s.len();
5+
let mut map = HashSet::new();
6+
let mut acc = 0;
7+
for c in s.chars() {
8+
// We added it a second time so we have a palindrome pair
9+
if !map.insert(c) {
10+
acc += 2;
11+
// Reset the map
12+
map.remove(&c);
13+
}
14+
}
15+
16+
// Case where we can add an extra character in the middle
17+
if len as i32 > acc {
18+
return acc + 1;
19+
}
20+
return acc;
21+
}
22+
23+
}

0 commit comments

Comments
 (0)