My goal is to return the longest palindrome in an array of bytes. My algorithm is:
Branch A
- From the first character to the last, is the string a palindrome? If so return it.
- If not, from the first character to the second last, is the string a palindrome? ...
- If not, from the first character to the third last, is the string a palindrome? ...
- ..
- Are there less than 2 characters left? if so, go to Branch A, but instead of reading from the first character, check from the second one.
- ..
- Are there less than 2 characters left? if so, go to Branch A, but instead of reading from the first character, check from the third one.
- ..
- Are there less than 2 characters left? Return `None.
This problem is iterative by nature but the algorithm doesn't seem to work in either my iterative or recursive solution. So far I've gotten to this:
// slices are upper-bound exclusive
// return expressions are optionally implicit
fn is_palindrome(bytes: &[u8]) -> bool {
if bytes.len() == 1 {
true
} else if bytes.len() == 2 {
bytes[0] == bytes[1]
} else if bytes[0] == bytes[bytes.len() - 1] {
is_palindrome(&bytes[1..bytes.len() - 1])
} else {
false
}
}
fn recurse_palindrome<'a>(s: &'a [u8], start: usize, end: usize) -> Option<&'a [u8]> {
if start == end {
if start == s.len() {
None
} else {
// if start < s.len() - 1
recurse_palindrome(&s, start + 1, s.len() - 1)
}
} else if is_palindrome(&s[start..end + 1]) {
Some(&s[start..end + 1])
} else {
recurse_palindrome(&s, start, end - 1)
}
}
pub fn longest_palindrome<'a>(s: &'a [u8]) -> Option<&'a [u8]> {
recurse_palindrome(&s, 0, s.len() - 1)
}
// expected answers and tests
#[cfg(test)]
mod test {
#[test]
fn is_palindrome() {
assert_eq!(super::is_palindrome(b"saas"), true); // ok
assert_eq!(super::is_palindrome(b"ss"), true); // ok
}
#[test]
fn longest_palindrome1() {
assert_eq!(
super::longest_palindrome(b"saasdbc").unwrap_or(b""),
b"saas"
); // passes
}
#[test]
fn longest_palindrome() {
assert_eq!(
super::longest_palindrome(b"ssaasdbc").unwrap_or(b""),
b"saas"
); // fails; returns b"ss" instead of b"saas"
}
}