Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get longest palindrome #704

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
* [Egg Dropping](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/egg_dropping.rs)
* [Fibonacci](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/fibonacci.rs)
* [Fractional Knapsack](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/fractional_knapsack.rs)
* [Get Length Of The Palindrome With Max Length](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/get_length_of_the_palindrome_with_max_length.rs)
* [Is Subsequence](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/is_subsequence.rs)
* [Knapsack](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/knapsack.rs)
* [Longest Common Subsequence](https://github.com/TheAlgorithms/Rust/blob/master/src/dynamic_programming/longest_common_subsequence.rs)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Find the longest palindrome substring in the string(non-unique solution)
Source:https://leetcode.cn/problems/longest-palindromic-substring/description/
Example:

Input: s = "abdbc"
Output: "bdb"
*/

pub fn get_length_of_the_palindrome_with_max_length(s: &str) -> String {
if s.is_empty() {
return "".to_string();
}
let s: Vec<char> = s.chars().collect();
let n = s.len();
let mut dp = vec![vec![true; n]; n];
// res is the index of the longest palindrome
let mut res = (0, 0);

// form filling strategy: fill in the form by
// use k instead of palindrome length
for k in 1..n {
for i in 0..(n - k) {
if k == 1 {
// strings of 2 equal characters are palindromes
dp[i][i + k] = s[i] == s[i + 1];
} else {
// a string that is equal on both sides and has a palindrome in the middle is also a palindrome
dp[i][i + k] = (s[i] == s[i + k]) && dp[i + 1][i + k - 1];
}

if dp[i][i + k] {
res = (i, i + k);
}
}
}
s[res.0..=res.1].iter().collect::<String>()
}

#[cfg(test)]
mod tests {
use super::get_length_of_the_palindrome_with_max_length;
macro_rules! test_get_length_of_the_palindrome_with_max_length {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
use crate::string::is_palindrome;
let (s, expected) = $inputs;
assert!(is_palindrome(expected));
assert_eq!(get_length_of_the_palindrome_with_max_length(s), expected);
assert_eq!(get_length_of_the_palindrome_with_max_length(expected), expected);
}
)*
}
}
test_get_length_of_the_palindrome_with_max_length! {
empty_input: ("", ""),
basic_1: ("abdbc", "bdb"),
basic_2: ("abyxycbabcyxy", "yxycbabcyxy"),
// Theoretically it is possible to return either aa or bb,
// there are multiple palindromes of the same length, here only the rightmost one is returned
basic_3: ("aabb", "bb"),
unicode_1: ("常威天天打来福", "天天"),
}
}
2 changes: 2 additions & 0 deletions src/dynamic_programming/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod coin_change;
mod egg_dropping;
mod fibonacci;
mod fractional_knapsack;
mod get_length_of_the_palindrome_with_max_length;
mod is_subsequence;
mod knapsack;
mod longest_common_subsequence;
Expand All @@ -28,6 +29,7 @@ pub use self::fibonacci::memoized_fibonacci;
pub use self::fibonacci::nth_fibonacci_number_modulo_m;
pub use self::fibonacci::recursive_fibonacci;
pub use self::fractional_knapsack::fractional_knapsack;
pub use self::get_length_of_the_palindrome_with_max_length::get_length_of_the_palindrome_with_max_length;
pub use self::is_subsequence::is_subsequence;
pub use self::knapsack::knapsack;
pub use self::longest_common_subsequence::longest_common_subsequence;
Expand Down