Skip to content

Commit 2d8802f

Browse files
committed
Add problem 2375: Construct Smallest Number From DI String
1 parent 94c5e8f commit 2d8802f

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,7 @@ pub mod problem_2369_check_if_there_is_a_valid_partition_for_the_array;
17681768
pub mod problem_2370_longest_ideal_subsequence;
17691769
pub mod problem_2373_largest_local_values_in_a_matrix;
17701770
pub mod problem_2374_node_with_highest_edge_score;
1771+
pub mod problem_2375_construct_smallest_number_from_di_string;
17711772
pub mod problem_2379_minimum_recolors_to_get_k_consecutive_black_blocks;
17721773
pub mod problem_2380_time_needed_to_rearrange_a_binary_string;
17731774
pub mod problem_2381_shifting_letters_ii;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn smallest_number(pattern: String) -> String {
7+
let n = pattern.len();
8+
let mut result = String::with_capacity(n + 1);
9+
let mut prev = b'I';
10+
let mut length = 0;
11+
let mut c = b'1';
12+
13+
for direction in pattern.bytes() {
14+
if direction == prev {
15+
length += 1;
16+
} else {
17+
if prev == b'I' {
18+
result.extend((c - length..c).map(char::from));
19+
length = 1;
20+
} else {
21+
result.extend((c - length..=c).map(char::from).rev());
22+
length = 0;
23+
}
24+
25+
prev = direction;
26+
}
27+
28+
c += 1;
29+
}
30+
31+
drop(pattern);
32+
33+
let iter = (c - length..=c).map(char::from);
34+
35+
if prev == b'I' {
36+
result.extend(iter);
37+
} else {
38+
result.extend(iter.rev());
39+
}
40+
41+
result
42+
}
43+
}
44+
45+
// ------------------------------------------------------ snip ------------------------------------------------------ //
46+
47+
impl super::Solution for Solution {
48+
fn smallest_number(pattern: String) -> String {
49+
Self::smallest_number(pattern)
50+
}
51+
}
52+
53+
#[cfg(test)]
54+
mod tests {
55+
#[test]
56+
fn test_solution() {
57+
super::super::tests::run::<super::Solution>();
58+
}
59+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn smallest_number(pattern: String) -> String {
7+
let n = pattern.len();
8+
let mut result = String::with_capacity(n + 1);
9+
let mut length = 0;
10+
let mut c = b'1';
11+
let mut iter = pattern.bytes();
12+
13+
'outer: loop {
14+
if let Some(direction) = iter.next() {
15+
if direction == b'I' {
16+
length += 1;
17+
c += 1;
18+
} else {
19+
result.extend((c - length..c).map(char::from));
20+
21+
length = 2;
22+
c += 2;
23+
24+
loop {
25+
if let Some(direction) = iter.next() {
26+
if direction == b'I' {
27+
break;
28+
}
29+
30+
length += 1;
31+
c += 1;
32+
} else {
33+
result.extend((c - length..c).map(char::from).rev());
34+
35+
break 'outer;
36+
}
37+
}
38+
39+
result.extend((c - length..c).map(char::from).rev());
40+
length = 0;
41+
}
42+
} else {
43+
result.extend((c - length..=c).map(char::from));
44+
45+
break;
46+
}
47+
}
48+
49+
drop(pattern);
50+
51+
result
52+
}
53+
}
54+
55+
// ------------------------------------------------------ snip ------------------------------------------------------ //
56+
57+
impl super::Solution for Solution {
58+
fn smallest_number(pattern: String) -> String {
59+
Self::smallest_number(pattern)
60+
}
61+
}
62+
63+
#[cfg(test)]
64+
mod tests {
65+
#[test]
66+
fn test_solution() {
67+
super::super::tests::run::<super::Solution>();
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod greedy;
2+
pub mod greedy_2;
3+
4+
pub trait Solution {
5+
fn smallest_number(pattern: String) -> String;
6+
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::Solution;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [("IIIDIDDD", "123549876"), ("DDD", "4321"), ("DDDIII", "4321567")];
14+
15+
for (pattern, expected) in test_cases {
16+
assert_eq!(S::smallest_number(pattern.to_string()), expected);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)