From b200c4032e3f58c5b812a94b87d6a00980fa36ed Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 4 Jun 2025 09:54:22 +0800 Subject: [PATCH] 20250604 finished. --- src/problem/mod.rs | 2 + ...aphically_largest_string_from_the_box_i.rs | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/problem/p3403_find_the_lexicographically_largest_string_from_the_box_i.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index be34bd7..3d2663d 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -690,3 +690,5 @@ mod p2359_find_closest_node_to_given_two_nodes; mod p2929_distribute_candies_among_children_ii; mod p1298_maximum_candies_you_can_get_from_boxes; + +mod p3403_find_the_lexicographically_largest_string_from_the_box_i; diff --git a/src/problem/p3403_find_the_lexicographically_largest_string_from_the_box_i.rs b/src/problem/p3403_find_the_lexicographically_largest_string_from_the_box_i.rs new file mode 100644 index 0000000..93f355f --- /dev/null +++ b/src/problem/p3403_find_the_lexicographically_largest_string_from_the_box_i.rs @@ -0,0 +1,48 @@ +/** + * [3403] Find the Lexicographically Largest String From the Box I + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn answer_string(word: String, num_friends: i32) -> String { + // When there is only one person, the word can not be split. + if num_friends == 1 { + return word; + } + + let word: Vec = word.bytes().collect(); + let length = word.len() - num_friends as usize + 1; + let mut result: Option<&[u8]> = None; + + for i in 0..word.len() { + let end = (i + length).min(word.len()); + let w = &word[i..(i + length).min(word.len())]; + + if let Some(r) = result { + if w > r { + result = Some(w) + } + } else { + result = Some(w); + } + } + + String::from_utf8(result.unwrap().into_iter().map(|x| *x).collect()).unwrap() + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3403() { + assert_eq!("gh", Solution::answer_string("gh".to_string(), 1)); + assert_eq!("dbc", Solution::answer_string("dbca".to_string(), 2)); + assert_eq!("g", Solution::answer_string("gggg".to_string(), 4)); + } +}