-
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 2399: Check Distances Between Same Letters
- Loading branch information
Showing
3 changed files
with
79 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
41 changes: 41 additions & 0 deletions
41
src/problem_2399_check_distances_between_same_letters/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,41 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn check_distances(s: String, distance: Vec<i32>) -> bool { | ||
let mut indices = [0_u8; 26]; | ||
let mut i = 0; | ||
|
||
s.bytes().all(|c| { | ||
let c = usize::from(c) - usize::from(b'a'); | ||
let state = &mut indices[c]; | ||
|
||
i += 1; | ||
|
||
if *state == 0 { | ||
*state = i; | ||
} else if i - *state - 1 != distance[c] as u8 { | ||
return false; | ||
} | ||
|
||
true | ||
}) | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn check_distances(s: String, distance: Vec<i32>) -> bool { | ||
Self::check_distances(s, distance) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/problem_2399_check_distances_between_same_letters/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,37 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn check_distances(s: String, distance: Vec<i32>) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
( | ||
( | ||
"abaccb", | ||
[ | ||
1, 3, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
], | ||
), | ||
true, | ||
), | ||
( | ||
( | ||
"aa", | ||
[ | ||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
], | ||
), | ||
false, | ||
), | ||
]; | ||
|
||
for ((s, distance), expected) in test_cases { | ||
assert_eq!(S::check_distances(s.to_string(), distance.to_vec()), expected); | ||
} | ||
} | ||
} |