Skip to content

Commit

Permalink
Add problem 1849: Splitting a String Into Descending Consecutive Values
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed May 2, 2024
1 parent 269e821 commit 6d9f5a1
Show file tree
Hide file tree
Showing 3 changed files with 113 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 @@ -1377,6 +1377,7 @@ pub mod problem_1844_replace_all_digits_with_characters;
pub mod problem_1845_seat_reservation_manager;
pub mod problem_1846_maximum_element_after_decreasing_and_rearranging;
pub mod problem_1848_minimum_distance_to_the_target_element;
pub mod problem_1849_splitting_a_string_into_descending_consecutive_values;
pub mod problem_1851_minimum_interval_to_include_each_query;
pub mod problem_1854_maximum_population_year;
pub mod problem_1855_maximum_distance_between_a_pair_of_values;
Expand Down
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>();
}
}
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);
}
}
}

0 comments on commit 6d9f5a1

Please sign in to comment.