Skip to content

Commit eaadf40

Browse files
committed
add solution
1 parent b5c3c25 commit eaadf40

File tree

1 file changed

+102
-3
lines changed
  • longest_palindromic_substring/src

1 file changed

+102
-3
lines changed

longest_palindromic_substring/src/lib.rs

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,123 @@
44
* [Problem link](https://leetcode.com/problems/longest-palindromic-substring/)
55
*/
66

7+
#![allow(dead_code)]
8+
79
struct Solution {}
810

911
// ----------------------------------------------------------------------------
1012

1113
impl Solution {
1214
pub fn longest_palindrome(s: String) -> String {
13-
"bab".to_string()
15+
if s.len() < 2 {
16+
return s;
17+
}
18+
let chars: Vec<char> = s.chars().collect();
19+
let odd_longest: String = {
20+
let mut longest_part = vec![];
21+
for (i, &c) in chars.iter().enumerate() {
22+
let mut current_part = vec![c];
23+
let (mut low, mut high) = (i, i);
24+
loop {
25+
if chars[low] != chars[high] {
26+
// not a palindrome
27+
break;
28+
}
29+
if low != high {
30+
// exclude the condition only one char
31+
current_part.push(chars[low]);
32+
}
33+
if low > 0 && high < chars.len() - 1 {
34+
low -= 1;
35+
high += 1;
36+
} else {
37+
break;
38+
}
39+
}
40+
if current_part.len() > longest_part.len() {
41+
longest_part = current_part;
42+
}
43+
}
44+
let left_part = longest_part[1..]
45+
.iter()
46+
.rev()
47+
.cloned()
48+
.collect::<Vec<char>>();
49+
[left_part, longest_part].concat().iter().cloned().collect()
50+
};
51+
let even_longest: String = {
52+
let mut longest_part = vec![];
53+
for (i, _) in chars[..chars.len() - 1].iter().enumerate() {
54+
let mut current_part = vec![];
55+
let (mut low, mut high) = (i, i + 1);
56+
loop {
57+
if chars[low] == chars[high] {
58+
current_part.push(chars[high]);
59+
} else {
60+
break;
61+
}
62+
if low > 0 && high < chars.len() - 1 {
63+
low -= 1;
64+
high += 1;
65+
} else {
66+
break;
67+
}
68+
}
69+
if current_part.len() > longest_part.len() {
70+
longest_part = current_part;
71+
}
72+
}
73+
let left_part = longest_part.iter().rev().cloned().collect::<Vec<char>>();
74+
[left_part, longest_part].concat().iter().cloned().collect()
75+
};
76+
if odd_longest.len() > even_longest.len() {
77+
odd_longest
78+
} else {
79+
even_longest
80+
}
1481
}
1582
}
1683

1784
#[cfg(test)]
1885
mod tests {
1986
use super::*;
2087

88+
fn accepted(input: String, expected: Vec<String>) {
89+
assert!(expected.contains(&Solution::longest_palindrome(input)));
90+
}
91+
2192
#[test]
2293
fn test_example_1() {
2394
let input = "babad".to_string();
24-
let outputs = vec!["bab".to_string(), "aba".to_string()];
25-
assert!(outputs.contains(&Solution::longest_palindrome(input)));
95+
let expected = vec!["bab".to_string(), "aba".to_string()];
96+
accepted(input, expected);
97+
}
98+
99+
#[test]
100+
fn test_example_2() {
101+
let input = "cbbd".to_string();
102+
let expected = vec!["bb".to_string()];
103+
accepted(input, expected);
104+
}
105+
106+
#[test]
107+
fn test_empty_string() {
108+
let input = "".to_string();
109+
let expected = vec!["".to_string()];
110+
accepted(input, expected);
111+
}
112+
113+
#[test]
114+
fn test_one_string() {
115+
let input = "a".to_string();
116+
let expected = vec!["a".to_string()];
117+
accepted(input, expected);
118+
}
119+
120+
#[test]
121+
fn test_two_string() {
122+
let input = "bb".to_string();
123+
let expected = vec!["bb".to_string()];
124+
accepted(input, expected);
26125
}
27126
}

0 commit comments

Comments
 (0)