-
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 2337: Move Pieces to Obtain a String
- Loading branch information
Showing
3 changed files
with
111 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,84 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl Solution { | ||
pub fn can_change(start: String, target: String) -> bool { | ||
let top = start.as_bytes(); | ||
let bottom = target.as_bytes(); | ||
let mut i = 0; | ||
let mut j = 0; | ||
|
||
while let Some(&y) = bottom.get(j) { | ||
match y { | ||
b'L' => loop { | ||
if let Some(&x) = top.get(i) { | ||
match x { | ||
b'L' => { | ||
if i < j { | ||
return false; | ||
} | ||
|
||
i += 1; | ||
|
||
break; | ||
} | ||
b'R' => return false, | ||
_ => i += 1, | ||
} | ||
} else { | ||
return false; | ||
} | ||
}, | ||
b'R' => { | ||
let window = &top[..=j]; | ||
|
||
loop { | ||
if let Some(&x) = window.get(i) { | ||
match x { | ||
b'L' => return false, | ||
b'R' => { | ||
i += 1; | ||
|
||
break; | ||
} | ||
_ => i += 1, | ||
} | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
_ => {} | ||
} | ||
|
||
j += 1; | ||
} | ||
|
||
while let Some(&c) = top.get(i) { | ||
if c != b'_' { | ||
return false; | ||
} | ||
|
||
i += 1; | ||
} | ||
|
||
true | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn can_change(start: String, target: String) -> bool { | ||
Self::can_change(start, target) | ||
} | ||
} | ||
|
||
#[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,26 @@ | ||
pub mod greedy; | ||
|
||
pub trait Solution { | ||
fn can_change(start: String, target: String) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(("_L__R__R_", "L______RR"), true), | ||
(("R_L_", "__LR"), false), | ||
(("_R", "R_"), false), | ||
(("_LL__R__R_", "L___L___RR"), false), | ||
(("_", "L"), false), | ||
(("L___L_", "_RR___"), false), | ||
(("_L__R__R_L", "L______RR_"), false), | ||
]; | ||
|
||
for ((start, target), expected) in test_cases { | ||
assert_eq!(S::can_change(start.to_string(), target.to_string()), expected); | ||
} | ||
} | ||
} |