forked from aylei/leetcode-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp0005_longest_palindromic_substring.rs
103 lines (95 loc) · 2.85 KB
/
p0005_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* [5] Longest Palindromic Substring
*
* Given a string s, return the longest palindromic substring in s.
*
*
* Example 1:
*
*
* Input: s = "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
*
* Example 2:
*
*
* Input: s = "cbbd"
* Output: "bb"
*
*
* Example 3:
*
*
* Input: s = "a"
* Output: "a"
*
*
* Example 4:
*
*
* Input: s = "ac"
* Output: "a"
*
*
*
* Constraints:
*
*
* 1 <= s.length <= 1000
* s consist of only digits and English letters (lower-case and/or upper-case),
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/longest-palindromic-substring/
// discuss: https://leetcode.com/problems/longest-palindromic-substring/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn longest_palindrome(s: String) -> String {
let mut longest_end_pos = 0usize;
let mut longest_len = 1usize;
let n : usize = s.len();
let mut last_start_positions : Vec<usize> = vec![];
let s : Vec<char> = s.chars().collect();
for i in 0..n {
let mut this_start : Vec<usize> = vec![];
this_start.push(i);
if 1 <= i {
if s[i-1] == s[i] {
this_start.push(i-1);
}
for &last_start_position in last_start_positions.iter() {
if 1 <= last_start_position && s[last_start_position - 1] == s[i] {
this_start.push(last_start_position - 1);
}
}
}
for &start_pos in this_start.iter() {
let palindrome_len : usize = i - start_pos + 1;
// println!("end(i)={}, start_pos={}, palindrome_len={},longest_len={}", i, start_pos, palindrome_len,longest_len);
if palindrome_len > longest_len {
longest_len = palindrome_len;
longest_end_pos = i;
}
}
last_start_positions = this_start;
}
let start_pos : usize = longest_end_pos + 1 - longest_len ; // inclusive
s.iter().skip(start_pos).take(longest_len).collect()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_5() {
assert_eq!(Solution::longest_palindrome("aaaaa".to_owned()), "aaaaa");
assert_eq!(Solution::longest_palindrome("babab".to_owned()), "babab");
assert_eq!(Solution::longest_palindrome("babcd".to_owned()), "bab");
assert_eq!(Solution::longest_palindrome("cbbd".to_owned()), "bb");
assert_eq!(Solution::longest_palindrome("bb".to_owned()), "bb");
assert_eq!(Solution::longest_palindrome("".to_owned()), "");
}
}