Skip to content

Commit

Permalink
Add problem 1844: Replace All Digits with Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Sep 15, 2023
1 parent 334fe10 commit f411c42
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,7 @@ pub mod problem_1832_check_if_the_sentence_is_pangram;
pub mod problem_1833_maximum_ice_cream_bars;
pub mod problem_1837_sum_of_digits_in_base_k;
pub mod problem_1839_longest_substring_of_all_vowels_in_order;
pub mod problem_1844_replace_all_digits_with_characters;

#[cfg(test)]
mod test_utilities;
34 changes: 34 additions & 0 deletions src/problem_1844_replace_all_digits_with_characters/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn replace_digits(s: String) -> String {
let mut s = s.into_bytes();
let mut i = 0;

while let Some([left, right]) = s.get_mut(i..i + 2) {
*right = *left + (*right - b'0');

i += 2;
}

String::from_utf8(s).unwrap()
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn replace_digits(s: String) -> String {
Self::replace_digits(s)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
18 changes: 18 additions & 0 deletions src/problem_1844_replace_all_digits_with_characters/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn replace_digits(s: String) -> String;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("a1c1e1", "abcdef"), ("a1b2c3d4e", "abbdcfdhe")];

for (s, expected) in test_cases {
assert_eq!(S::replace_digits(s.to_string()), expected);
}
}
}

0 comments on commit f411c42

Please sign in to comment.