-
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 2381: Shifting Letters II
- Loading branch information
Showing
3 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 @@ | ||
pub mod sweep_line; | ||
|
||
pub trait Solution { | ||
fn shifting_letters(s: String, shifts: Vec<Vec<i32>>) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("abc", &[[0, 1, 0], [1, 2, 1], [0, 2, 1]] as &[_]), "ace"), | ||
(("dztz", &[[0, 0, 0], [1, 1, 1]]), "catz"), | ||
]; | ||
|
||
for ((s, shifts), expected) in test_cases { | ||
assert_eq!( | ||
S::shifting_letters(s.to_string(), shifts.iter().map(Vec::from).collect()), | ||
expected, | ||
); | ||
} | ||
} | ||
} |
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,80 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
fn assign_add(target: &mut u8, value: u8) { | ||
*target += value; | ||
|
||
if *target >= 26 { | ||
*target -= 26; | ||
} | ||
} | ||
|
||
fn assign_sub(target: &mut u8, value: u8) { | ||
if *target < value { | ||
*target += 26; | ||
} | ||
|
||
*target -= value; | ||
} | ||
|
||
fn helper(slice: &mut [u8], shifts: &[Vec<i32>]) { | ||
let mut prev = b'a'; | ||
|
||
for offset in &mut *slice { | ||
let current = *offset; | ||
|
||
Self::assign_sub(offset, prev); | ||
|
||
prev = current; | ||
} | ||
|
||
for shift in shifts { | ||
let [start, end, direction]: [_; 3] = shift.as_slice().try_into().ok().unwrap(); | ||
let diff = if direction == 0 { 25 } else { 1 }; | ||
|
||
Self::assign_add(&mut slice[start as u32 as usize], diff); | ||
|
||
if let Some(end) = slice.get_mut(end as u32 as usize + 1) { | ||
Self::assign_sub(end, diff); | ||
} | ||
} | ||
|
||
let mut prev = b'a'; | ||
|
||
for target in slice { | ||
prev += *target; | ||
|
||
if prev > b'z' { | ||
prev -= 26; | ||
} | ||
|
||
*target = prev; | ||
} | ||
} | ||
|
||
pub fn shifting_letters(s: String, shifts: Vec<Vec<i32>>) -> String { | ||
let mut buffer = s.into_bytes(); | ||
|
||
Self::helper(&mut buffer, &shifts); | ||
|
||
String::from_utf8(buffer).unwrap() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn shifting_letters(s: String, shifts: Vec<Vec<i32>>) -> String { | ||
Self::shifting_letters(s, shifts) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |