-
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 2075: Decode the Slanted Ciphertext
- Loading branch information
Showing
3 changed files
with
61 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
37 changes: 37 additions & 0 deletions
37
src/problem_2075_decode_the_slanted_ciphertext/iterative.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,37 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn decode_ciphertext(encoded_text: String, rows: i32) -> String { | ||
let rows = rows as u32 as usize; | ||
let columns = encoded_text.len() / rows; | ||
let mut result = Vec::with_capacity(encoded_text.len()); | ||
let mut iter = encoded_text.bytes(); | ||
|
||
for _ in 0..columns { | ||
result.extend(iter.clone().step_by(columns + 1)); | ||
iter.next(); | ||
} | ||
|
||
result.truncate(result.iter().rposition(|&c| c != b' ').map_or(0, |i| i + 1)); | ||
|
||
String::from_utf8(result).unwrap() | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn decode_ciphertext(encoded_text: String, rows: i32) -> String { | ||
Self::decode_ciphertext(encoded_text, rows) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
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,23 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn decode_ciphertext(encoded_text: String, rows: i32) -> String; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("ch ie pr", 3), "cipher"), | ||
(("iveo eed l te olc", 4), "i love leetcode"), | ||
(("coding", 1), "coding"), | ||
(("", 5), ""), | ||
]; | ||
|
||
for ((encoded_text, rows), expected) in test_cases { | ||
assert_eq!(S::decode_ciphertext(encoded_text.to_string(), rows), expected); | ||
} | ||
} | ||
} |