Skip to content

Commit e2206a6

Browse files
authored
Merge pull request #18 from jtr109/5-longest-palindromic-substring
5 longest palindromic substring
2 parents 8e66950 + eaadf40 commit e2206a6

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ members = [
99
"implement_queue_using_stacks",
1010
"merge_sorted_array",
1111
"intersection_of_two_arrays",
12+
"longest_palindromic_substring",
1213
]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Resolving problems of LeetCode in RustLang.
88
## Solutions
99

1010
* [1. Two Sum](./two_sum/src/lib.rs)
11+
* [5. Longest Palindromic Substring](./longest_palindromic_substring/lib.rs)
1112
* [7. Reverse Integer](./reverse_integer/src/lib.rs)
1213
* [15. 3 Sum](./three_sum/src/lib.rs)
1314
* [88. Merge Sorted Array](./merge_sorted_array/src/lib.rs)
@@ -25,3 +26,5 @@ cargo doc --open -p <package_name>
2526
## References
2627

2728
* [LeetcodeTop](https://github.com/afatcoder/LeetcodeTop): Top Hit Leetcode problems in interviews.
29+
* [The Algorithms - Rust](https://github.com/TheAlgorithms/Rust)
30+
* [LeetCode Cookbook](https://books.halfrost.com/leetcode/)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "longest_palindromic_substring"
3+
version = "0.1.0"
4+
authors = ["Ryan Li <conbas2019@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*!
2+
* # 5. Longest Palindromic Substring
3+
*
4+
* [Problem link](https://leetcode.com/problems/longest-palindromic-substring/)
5+
*/
6+
7+
#![allow(dead_code)]
8+
9+
struct Solution {}
10+
11+
// ----------------------------------------------------------------------------
12+
13+
impl Solution {
14+
pub fn longest_palindrome(s: String) -> 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+
}
81+
}
82+
}
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
fn accepted(input: String, expected: Vec<String>) {
89+
assert!(expected.contains(&Solution::longest_palindrome(input)));
90+
}
91+
92+
#[test]
93+
fn test_example_1() {
94+
let input = "babad".to_string();
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);
125+
}
126+
}

0 commit comments

Comments
 (0)