-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from SKTT1Ryze/longest_palindromic_substring
Longest palindromic substring
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters