20250330 finished.

This commit is contained in:
jackfiled 2025-03-30 14:21:30 +08:00
parent 0b20b8fa9c
commit d9b5ef618f
2 changed files with 52 additions and 0 deletions

View File

@ -568,3 +568,5 @@ mod p2712_minimum_cost_to_make_all_characters_equal;
mod p2716_minimize_string_length;
mod p2360_longest_cycle_in_a_graph;
mod p2109_adding_spaces_to_a_string;

View File

@ -0,0 +1,50 @@
/**
* [2109] Adding Spaces to a String
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn add_spaces(s: String, spaces: Vec<i32>) -> String {
let s: Vec<char> = s.chars().collect();
let mut result = Vec::with_capacity(s.len() + spaces.len());
let mut iter = spaces.into_iter().peekable();
for (i, v) in s.into_iter().enumerate() {
if let Some(&pos) = iter.peek() {
if i == pos as usize {
iter.next();
result.push(' ');
}
}
result.push(v);
}
result.into_iter().collect()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2109() {
assert_eq!(
"Leetcode Helps Me Learn",
Solution::add_spaces("LeetcodeHelpsMeLearn".to_owned(), vec![8, 13, 15])
);
assert_eq!(
"i code in py thon",
Solution::add_spaces("icodeinpython".to_owned(), vec![1, 5, 7, 9])
);
assert_eq!(
" s p a c i n g",
Solution::add_spaces("spacing".to_owned(), vec![0, 1, 2, 3, 4, 5, 6])
);
}
}