-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2131: Longest Palindrome by Concatenating Two Letter Words
- Loading branch information
Showing
3 changed files
with
86 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
60 changes: 60 additions & 0 deletions
60
src/problem_2131_longest_palindrome_by_concatenating_two_letter_words/greedy.rs
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,60 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn longest_palindrome(words: Vec<String>) -> i32 { | ||
let mut counts = [0_u32; 676]; | ||
let mut result = 0_u32; | ||
let mut unpaired = 0_u32; | ||
|
||
for word in words { | ||
let [c0, c1]: [_; 2] = word.into_bytes().try_into().ok().unwrap(); | ||
let (c0, c1) = (u16::from(c0 - b'a'), u16::from(c1 - b'a')); | ||
|
||
if c0 == c1 { | ||
let count = &mut counts[usize::from(26 * c0 + c1)]; | ||
|
||
if *count == 0 { | ||
unpaired += 1; | ||
*count += 1; | ||
} else { | ||
unpaired -= 1; | ||
*count -= 1; | ||
result += 4; | ||
} | ||
} else { | ||
let other_count = &mut counts[usize::from(26 * c1 + c0)]; | ||
|
||
if *other_count == 0 { | ||
counts[usize::from(26 * c0 + c1)] += 1; | ||
} else { | ||
*other_count -= 1; | ||
result += 4; | ||
} | ||
} | ||
} | ||
|
||
if unpaired != 0 { | ||
result += 2; | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn longest_palindrome(words: Vec<String>) -> i32 { | ||
Self::longest_palindrome(words) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/problem_2131_longest_palindrome_by_concatenating_two_letter_words/mod.rs
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,25 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn longest_palindrome(words: Vec<String>) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&["lc", "cl", "gg"] as &[_], 6), | ||
(&["ab", "ty", "yt", "lc", "cl", "ab"], 8), | ||
(&["cc", "ll", "xx"], 2), | ||
]; | ||
|
||
for (words, expected) in test_cases { | ||
assert_eq!( | ||
S::longest_palindrome(words.iter().copied().map(str::to_string).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |