-
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 1849: Splitting a String Into Descending Consecutive Values
- Loading branch information
Showing
3 changed files
with
113 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
86 changes: 86 additions & 0 deletions
86
src/problem_1849_splitting_a_string_into_descending_consecutive_values/backtracking.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,86 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
use std::str::Bytes; | ||
|
||
impl Solution { | ||
fn check(mut first: u8, mut iter: Bytes, mut expected: u64) -> bool { | ||
while expected != 0 { | ||
let mut value = u64::from(first - b'0'); | ||
|
||
loop { | ||
match value.cmp(&expected) { | ||
Ordering::Less => { | ||
if let Some(next) = iter.next() { | ||
value = value * 10 + u64::from(next - b'0'); | ||
} else { | ||
return false; | ||
} | ||
} | ||
Ordering::Equal => { | ||
if let Some(next) = iter.next() { | ||
first = next; | ||
expected -= 1; | ||
|
||
break; | ||
} | ||
|
||
return true; | ||
} | ||
Ordering::Greater => return false, | ||
} | ||
} | ||
} | ||
|
||
first == b'0' && iter.all(|c| c == b'0') | ||
} | ||
|
||
pub fn split_string(s: String) -> bool { | ||
let mut iter = s.bytes(); | ||
|
||
let mut value = loop { | ||
if let Some(c) = iter.next() { | ||
if c != b'0' { | ||
break u64::from(c - b'0'); | ||
} | ||
} else { | ||
return false; | ||
} | ||
}; | ||
|
||
while let Some(c) = iter.next() { | ||
if Self::check(c, iter.clone(), value - 1) { | ||
return true; | ||
} | ||
|
||
if let Some(next_value) = value | ||
.checked_mul(10) | ||
.and_then(|value| value.checked_add(u64::from(c - b'0'))) | ||
{ | ||
value = next_value; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
false | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn split_string(s: String) -> bool { | ||
Self::split_string(s) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/problem_1849_splitting_a_string_into_descending_consecutive_values/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,26 @@ | ||
pub mod backtracking; | ||
|
||
pub trait Solution { | ||
fn split_string(s: String) -> bool; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
("1234", false), | ||
("050043", true), | ||
("9080701", false), | ||
("99999999999999999998", true), | ||
("00000", false), | ||
("64424509442147483647", false), | ||
("10009998", true), | ||
]; | ||
|
||
for (s, expected) in test_cases { | ||
assert_eq!(S::split_string(s.to_string()), expected); | ||
} | ||
} | ||
} |