-
Notifications
You must be signed in to change notification settings - Fork 21
/
iterative.rs
51 lines (41 loc) · 1.32 KB
/
iterative.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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
use std::cmp::Ordering;
impl Solution {
pub fn last_substring(palindrome: String) -> String {
let s = palindrome.as_bytes();
let mut best_start = 0;
let mut start = 1;
let mut length = 0;
while let Some(&c) = s.get(start + length) {
match c.cmp(&s[best_start + length]) {
Ordering::Less => {
start += length + 1;
length = 0;
}
Ordering::Equal => length += 1,
Ordering::Greater => {
best_start = start.max(best_start + length + 1);
start = best_start + 1;
length = 0;
}
}
}
let mut result = palindrome;
result.replace_range(..best_start, "");
result
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn last_substring(s: String) -> String {
Self::last_substring(s)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}