-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
70 lines (57 loc) · 2.89 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// https://leetcode.com/problems/longest-palindromic-substring/
pub struct Solution;
impl Solution {
pub fn longest_palindrome(s: String) -> String {
use std::str;
if s.is_empty() || s.len() == 1 {
return s;
}
for size in (1..=s.len()).rev() {
if let Some(palindrome) = s.as_bytes().windows(size).find(Self::is_palindrome) {
// note: unwrap is save here cause `s` contains only english letters and digits
let palindrome = str::from_utf8(palindrome).unwrap();
return String::from(palindrome);
}
}
String::from("")
}
/// Operates on slice of slice
fn is_palindrome(&substr: &&[u8]) -> bool {
let reversed_substr = substr.iter().rev();
let mut both_ends = substr.iter().zip(reversed_substr);
// b"121" -> (b'1', b'1'), (b'2', b'2'), (b'1', b'1') -> true
// b"123" -> (b'1', b'3'), stop processing -> false
both_ends.all(|(left, right)| left == right)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example1() {
assert!(
Solution::longest_palindrome("babad".to_string()) == "bab".to_string()
|| Solution::longest_palindrome("babad".to_string()) == "aba".to_string()
);
}
#[test]
fn example2() {
assert!(Solution::longest_palindrome("cbbd".to_string()) == "bb".to_string());
}
#[test]
fn example3() {
assert!(Solution::longest_palindrome("".to_string()) == "".to_string());
}
#[test]
fn example4() {
assert!(Solution::longest_palindrome("f".to_string()) == "f".to_string());
}
#[test]
fn example5() {
assert!(Solution::longest_palindrome("omoromo".to_string()) == "omoromo".to_string());
}
#[test]
fn example6() {
assert!(Solution::longest_palindrome("civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth".to_string()) == "ranynar".to_string());
}
}