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

Longest palindromic substring #10

Merged
merged 3 commits into from
Oct 15, 2023
Merged
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 leetcode/src/problems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod add_two_numbers;
pub mod longest_palindromic_substring;
pub mod longest_substring;
pub mod median_of_two_sorted_arrays;
pub mod two_sum;
Expand Down
24 changes: 24 additions & 0 deletions leetcode/src/problems/longest_palindromic_substring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use super::{Difficulty, Problem, Topic};

pub struct LongestPalindromicSubstring;

impl Problem for LongestPalindromicSubstring {
fn id(&self) -> usize {
5
}
fn difficulty(&self) -> Difficulty {
Difficulty::Medium
}
fn topic(&self) -> Topic {
Topic::Algorithms
}
fn title(&self) -> String {
"Longest Palindromic Substring".into()
}
fn description(&self) -> String {
r#"Given a string s, return the longest palindromic substring in s."#.into()
}
fn labels(&self) -> Vec<String> {
["String".into(), "DP".into()].into()
}
}
1 change: 1 addition & 0 deletions leetcode/src/solutions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;

pub mod add_two_numbers;
pub mod longest_palindromic_substring;
pub mod longest_substring;
pub mod median_of_two_sorted_arrays;
pub mod two_sum;
Expand Down
73 changes: 73 additions & 0 deletions leetcode/src/solutions/longest_palindromic_substring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::Solution;
use std::collections::HashMap;

pub struct LongestPalindromicSubstring;

impl Solution for LongestPalindromicSubstring {
fn name(&self) -> String {
"Solution for Longest Palindromic Substring".into()
}
fn problem_id(&self) -> usize {
5
}
fn location(&self) -> String {
crate::location!()
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [("babad", "bab"), ("cbbd", "bb")];

for (input, expect) in testcases {
let output = Self::longest_palindrome(input.into());

if expect != output {
anyhow::bail!("test failed for input={input}, expect={expect}, output={output}");
}
}

Ok(())
}
fn benchmark(&self) -> anyhow::Result<usize> {
todo!()
}
}

impl LongestPalindromicSubstring {
pub fn longest_palindrome(s: String) -> String {
let mut max = "";
let mut map: HashMap<char, Vec<_>> = HashMap::new();

for (end, ch) in s.chars().enumerate() {
map.entry(ch).or_default().push(end);

for &start in map.get(&ch).unwrap_or(&vec![]) {
let subs = &s[start..end + 1];

if Self::check_palindromic(subs) {
if subs.len() > max.len() {
max = subs;
}
break;
}
}
}

max.into()
}

fn check_palindromic(s: &str) -> bool {
let chars: Vec<_> = s.chars().collect();
let len = chars.len();
let mut p1 = 0;
let mut p2 = len - 1;

while p1 < p2 {
if chars[p1] != chars[p2] {
return false;
}
p1 += 1;
p2 -= 1;
}

true
}
}
6 changes: 6 additions & 0 deletions runtime/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub fn register_all(handle: ContainerHandle) -> anyhow::Result<()> {
handle.register_problem(|_| problems::median_of_two_sorted_arrays::MedianOfTwoSortedArrays)?;
handle
.register_solution(|_| solutions::median_of_two_sorted_arrays::MedianOfTwoSortedArrays)?;
handle.register_problem(|_| {
problems::longest_palindromic_substring::LongestPalindromicSubstring
})?;
handle.register_solution(|_| {
solutions::longest_palindromic_substring::LongestPalindromicSubstring
})?;

Ok(())
}