20250604 finished.
This commit is contained in:
parent
2ce7e6667f
commit
b200c4032e
|
@ -690,3 +690,5 @@ mod p2359_find_closest_node_to_given_two_nodes;
|
||||||
mod p2929_distribute_candies_among_children_ii;
|
mod p2929_distribute_candies_among_children_ii;
|
||||||
|
|
||||||
mod p1298_maximum_candies_you_can_get_from_boxes;
|
mod p1298_maximum_candies_you_can_get_from_boxes;
|
||||||
|
|
||||||
|
mod p3403_find_the_lexicographically_largest_string_from_the_box_i;
|
||||||
|
|
|
@ -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<u8> = 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user