Skip to content

Commit

Permalink
Merge pull request #10 from SKTT1Ryze/longest_palindromic_substring
Browse files Browse the repository at this point in the history
Longest palindromic substring
  • Loading branch information
SKTT1Ryze authored Oct 15, 2023
2 parents 28f2fdf + 3f07b23 commit 52fd596
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
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(())
}

0 comments on commit 52fd596

Please sign in to comment.