-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlongest-palindromic-substring.rs
51 lines (40 loc) · 1.21 KB
/
longest-palindromic-substring.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
use std::f32::consts::E;
fn main() {
assert_eq!(
Solution::longest_palindrome("aaaa".into()),
"aaaa".to_string()
);
}
struct Solution;
impl Solution {
pub fn longest_palindrome(s: String) -> String {
let length = s.len();
if length == 1 {
return s;
}
let bytes = s.as_bytes();
let mut v: Vec<Vec<bool>> = (0..length)
.map(|x| (0..length).map(|y| x == y).collect::<Vec<bool>>())
.collect();
let (mut start, mut end) = (0, 0);
for i in 1..length {
for start1 in 0..length {
let end1 = start1 + i;
if end1 >= length {
break;
}
if i == 1 {
v[start1][end1] = bytes[start1] == bytes[end1];
} else {
v[start1][end1] = bytes[start1] == bytes[end1] && v[start1 + 1][end1 - 1];
}
if end1 - start1 >= end - start {
start = start1;
end = end1;
}
}
}
s[start..=end].to_owned()
}
}