24 lines
746 B
Rust
24 lines
746 B
Rust
pub fn longest_palindrome(value: &str) -> String {
|
|
let mut lp: String = "0".to_string();
|
|
let input_size = value.len() - 1;
|
|
|
|
for pal_size in 2..=input_size {
|
|
for i in 0..input_size {
|
|
if input_size - i < pal_size {
|
|
break;
|
|
}
|
|
if let Some(slice) = value.get(i..(i + pal_size - 1)) {
|
|
// let reverted_slice = slice.chars().rev().collect::<String>();
|
|
// if string was mutable, we count use .reserve()
|
|
let reverted_slice = slice.chars().rev().collect::<String>();
|
|
if slice == reverted_slice {
|
|
lp = reverted_slice;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
lp
|
|
}
|